Live data from Hacker News

FizzBuzz in ten languages

iolivia.me

51–60 of 103 posts

Re: FizzBuzz in ten languages

#52
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.

You can name things like

    (\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

#53

Why 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?

A few others that are similar:

- 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

#56
The python indentation is inconsistent - two spaces at the first level of indentation, and six spaces at the second level of indentation.

I didn't think python would allow this, but apparently it does!

Re: FizzBuzz in ten languages

#57

Why 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.

I wonder how widespread it's become in shitty prepare-for-coding-interview materials, so variations might be needed, but as a tool for identifying non-programmers, it is extremely good. It's ridiculous how good it is, it really shouldn't be, because as a decent programmer, it's so hard to understand how anyone could apply for a programming job, yet still be so bad at programming that they can't pass the test.

And yet, there are so many people like that it's fucking scary.

Re: FizzBuzz in ten languages

#59
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…

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…

The lambda calculus approach provides an answer that is a theoretical solution for multiple programming languages. With very minor changes the same answer can apply to C Lang or JavaScript.

Re: FizzBuzz in ten languages

#60

Earlier 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()

Extra points for deviousness, but this won't produce the correct visual result when logged in on a hardcopy terminal (e.g. DECwriter); or even on a glass tty (e.g. DEC VT100) if you set the upper limit to 10001. It will also always fail a correctness test of diffing against the proper output. You should definitely consider entering the Obfuscated C contest, though.
Post reply on HN