FizzBuzz in ten languages
51–60 of 103 posts
Re: FizzBuzz in ten languages
#52Took 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.
(\id. ...) (\x. x)
This names the identity function id. You can make this slightly prettier like (\let. ...) (\def body. body def)
Which lets you do let (\x. x) \id. ...
Booleans are also functions let (\t f. t) \true.
let (\t f. f) \false.
let (\x. x) \then.
let (\x. x) \else.
let (\b thenLit ift elseLit iff. b ift iff) \if.
Which lets you write if true then foo else bar
Where then and else are thrown away, without them it is basically lisp syntax. You could make this slightly fancier - translate into cps to support `else if` and require `end` at the end.Tl;dr: with hacks
Re: FizzBuzz in ten languages
#53Why or how did fizzbuzz earn its place in lore? I looked at Wikipedia and it didn't seem to go into detail. Also we have the teapot, hello world, Foo Bar What are some others?
- fibonacci sequence to n (ooh, look, isn't recursion pretty) - parallel map (simple demonstration of parallelism in languages that allow it)
[slightly more involved, webapp hello worlds] - basic blog (various MVC web frameworks, normally along lines of if I run commands x, y and z I get models, views and controllers and it all works) - basic todo application (various frontend frameworks) - basic chat application (as a realtime demonstration, generally for websockets)
Re: FizzBuzz in ten languages
#54Re: FizzBuzz in ten languages
#55Re: FizzBuzz in ten languages
#56I didn't think python would allow this, but apparently it does!
Re: FizzBuzz in ten languages
#57Why or how did fizzbuzz earn its place in lore? I looked at Wikipedia and it didn't seem to go into detail. Also we have the teapot, hello world, Foo Bar What are some others?
Why or how did fizzbuzz earn its place in lore? The functional why was as a filter for eliminating impostors. At the time it was new, no one could look up the answers for it, because it was such an arbitrary problem. Now, it's just a curiosity.
And yet, there are so many people like that it's fucking scary.
Re: FizzBuzz in ten languages
#58Re: FizzBuzz in ten languages
#59Took 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…
The whole point of fizzbuzz is to filter for people that can just solve a tiny problem in a reasonably short time-- that's it. Most interviewers will even give some slack if the person has to look up the modulo operator or even do with out it. If someone can't do it at all there's some kind of serious issue with the way they're approaching the problem, or perhaps they're just not capable of doing it. But another comm…
Re: FizzBuzz in ten languages
#60Earlier quoted context omitted.
The "Haskell implementation using monoids" on this page is the only solution I've seen where adding a new behavior (e.g. "FizzBuzzWoof") only requires a modification in one spot. (d 3 "Fizz" d 5 "Buzz") becomes (d 3 "Fizz" d 5 "Buzz" d 13 "Woof") eta: And just below it is a similar solution in Python. Cool!
With similar properties, but more disgusting: for n in range(1, 101): print(n, end='\r') if n % 3 == 0: print("Fizz", end='') if n % 5 == 0: print("Buzz", end='') print()