Live data from Hacker News

FizzBuzz in ten languages

iolivia.me

61–70 of 103 posts

Re: FizzBuzz in ten languages

#61
post #28

Your bash example could be a little cleaner. Since 0 is truthy in bash and [ `expr something` ] is, for most purposes, (( )). for i in {1..100}; do if (( $i % 3 && $i % 5 )); then echo FizzBuzz elif (( $i % 3 )); then echo Fizz elif (( $i % 5 )); then echo Buzz else echo $i fi done Bash also has a decently powerful matching statement so you can do something similar to the rust example. for i in {1..100}; do case "$((…

Bash exit code 0 is truthy, but that's not how the if statement works:

if ((0)); then echo 0=true; else echo 0=false; fi

if ((1)); then echo 1=true; else echo 1=false; fi

# output

0=false

1=true

To work as expected, the if statements should be rewritten as

if ((i % 3 == 0 && i % 5 == 0)); then echo FizzBuzz

and so on.

Re: FizzBuzz in ten languages

#63

I've been looking for an example that shows how pattern matching can make code much both compact and readable. I think this does nicely. Here's an example in Scala: (1 to 100).map(i => (i % 3, i % 5) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => s"$i" }).foreach(println) Compare that to the rest of the examples on the page. The only one that comes close in either readability…

Maybe I'm missing something, but how is that any more readable than:

  for val in xrange(1, 100):
    if val % 15 == 0:
      print "FizzBuzz"
    elif val % 5 == 0:
      print "Buzz"
    elif val % 3 == 0:
      print "Fizz"
    else:
      print val

Re: FizzBuzz in ten languages

#64
post #47
post #28

Your bash example could be a little cleaner. Since 0 is truthy in bash and [ `expr something` ] is, for most purposes, (( )). for i in {1..100}; do if (( $i % 3 && $i % 5 )); then echo FizzBuzz elif (( $i % 3 )); then echo Fizz elif (( $i % 5 )); then echo Buzz else echo $i fi done Bash also has a decently powerful matching statement so you can do something similar to the rust example. for i in {1..100}; do case "$((…

#/bin/bash for n in {1..100} ; do f="" ((n % 3)) || f="Fizz" ((n % 5)) || f="${f}Buzz" echo ${f:-$n} done

More bikeshedding for the new year:

  #!/bin/bash
  for n in {1..100}; do
    f=
    ((n % 3)) || f=Fizz
    ((n % 5)) || f+=Buzz
    echo ${f:-$n}
  done

Re: FizzBuzz in ten languages

#66
post #4

Crossposting my comment from the blog: The Rust example looks a bit strange, since you're matching on a variable, but don't actually use it. Instead, you could write the match like this: let div_by_three = x % 3 == 0; let div_by_five = x % 5 == 0; match (div_by_three, div_by_five) { (true, true) => println!("FizzBuzz"), (true, false) => println!("Fizz"), (false, true) => println!("Buzz"), _ => println!("{}", x), } ..…

That looks so much like Elixir.

    for x  IO.puts("FizzBuzz")
        [0, _] -> IO.puts("Fizz")
        [_, 0] -> IO.puts("Buzz")
        _ -> x
      end
    end

Re: FizzBuzz in ten languages

#67
post #10

FizzBuzz Enterprise: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...

Given how similar the two languages are, both in syntax and culture, it's no surprise there's a C# version too:

https://github.com/jongeorge1/FizzBuzzEnterpriseEdition-CSha...

Re: FizzBuzz in ten languages

#68
post #12

Took this as an excuse to write it in pure lambda calculus https://gist.github.com/Tarmean/65da65b8da37bd66d48e96d2aa73... I always enjoy how lambda calculus suddenly becomes a readable language after the prelude: let (\n. let (\m. isZero (mod n m)) \divisibleBy. if (and (divisibleBy 3) (divisibleBy 5)) then FizzBuzz else (if (divisibleBy 3) then Fizz else (if (divisibleBy 5) then Buzz else (intToStr n)))) \fizzBuzzS…

I'm a bit confused, how did you implement let, if, else etc.? I don't really know much about the lambda calculus except for what I learned from a short youtube video.

There's a good elaboration of it in Tom Stuart's "Programming With Nothing" talk[0], in which he does a lambda calculus implementation of FizzBuzz in what's left of Ruby if you leave everything out except the things strictly necessary to do lambda calculus - with the exception of what you could call C-style macros (basically text search-and-replace, so you can call a section of code FOO and just type FOO everywhere you would have typed that section of code). There's also the little matter of output - you need to translate the resulting Church numerals into readable form for those who can't see that you've obviously produced correct FizzBuzz. There's a good enough explanation that what needs to be sort of hand-waved for brevity (things that are shown but not really explained in detail) aren't entirely opaque - you will be able to figure it out.

Keep in mind, too, that the lambda calculus is a lot like machine language in one respect - the same little bit of "code" can have more than one meaning to the programmer even if the "machine" is doing exactly the same thing.

[0] https://www.youtube.com/watch?v=VUhlNx_-wYk

Re: FizzBuzz in ten languages

#69
post #63

I've been looking for an example that shows how pattern matching can make code much both compact and readable. I think this does nicely. Here's an example in Scala: (1 to 100).map(i => (i % 3, i % 5) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => s"$i" }).foreach(println) Compare that to the rest of the examples on the page. The only one that comes close in either readability…

Maybe I'm missing something, but how is that any more readable than: for val in xrange(1, 100): if val % 15 == 0: print "FizzBuzz" elif val % 5 == 0: print "Buzz" elif val % 3 == 0: print "Fizz" else: print val

[deleted]

Re: FizzBuzz in ten languages

#70
post #63

I've been looking for an example that shows how pattern matching can make code much both compact and readable. I think this does nicely. Here's an example in Scala: (1 to 100).map(i => (i % 3, i % 5) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => s"$i" }).foreach(println) Compare that to the rest of the examples on the page. The only one that comes close in either readability…

Maybe I'm missing something, but how is that any more readable than: for val in xrange(1, 100): if val % 15 == 0: print "FizzBuzz" elif val % 5 == 0: print "Buzz" elif val % 3 == 0: print "Fizz" else: print val

Well for one, the cases of a match statement are guaranteed to use the same variables and return the same output, in every case — the equivalent if-elseif-else does not offer the same guarantees, and has to be read in full to reach the same conclusion

So the statements are equivalent in whole, but the difference is that a match statement requires less reading (as it holds more meaning)

Post reply on HN