FizzBuzz in ten languages
11–20 of 103 posts
Re: FizzBuzz in ten languages
#12I 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))))
\fizzBuzzStep.
(loop \recurse. \n.
if (equals n 100)
then nil
else (cons (fizzBuzzStep n) (recurse (n+1)))
) 1)
(\letArg letBody. letBody letArg)
let, if, then, else, loop, and even the numbers are all functions.Re: FizzBuzz in ten languages
#13 (defn fizzbuzz [x]
(cond-> nil
(zero? (mod x 3)) (str "Fizz")
(zero? (mod x 5)) (str "Buzz")
:always (or (str x))))
(doseq [x (range 1 101)] (prn (fizzbuzz x)))Re: FizzBuzz in ten languages
#14Took 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…
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 common way for people to fail or at least get red-flagged is to over-think it and come up with a turgid solution-- like using lambda calculus!
Simple problem. Simple solution. Nothing fancy. Going against that is asking for failure.
Re: FizzBuzz in ten languages
#15Took 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
#16The next question I like to ask in an interview, once they have fizzbuzz working is, "Now make it more efficient."
Re: FizzBuzz in ten languages
#17The next question I like to ask in an interview, once they have fizzbuzz working is, "Now make it more efficient."
echo '#include ' > fb.c
./fizzbuzz > buf
xxd -i buf >> fb.c
echo 'int main(void) { return fwrite(buf, buf_len, 1, stdout) != 1; }' >> fb.cRe: FizzBuzz in ten languages
#18 (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 or compactness (in my opinion) is the Rust example, mainly because it's syntactically almost identical, just a bit more verbose. I'm really excited that C# 8 will support similar syntax with _ discards.Re: FizzBuzz in ten languages
#19The next question I like to ask in an interview, once they have fizzbuzz working is, "Now make it more efficient."
const std = @import("std");
pub fn main() u8 {
@setEvalBranchQuota(2000);
const precomputed_output = comptime fizzbuzz: {
var s: []const u8 = "";
var i: usize = 1;
while (i
Output: $ zig build-exe fizzbuzz.zig
$ ./fizzbuzz
$ strace ./fizzbuzz
execve("./fizzbuzz", ["./fizzbuzz"], 0x7ffeeb1a4540 /* 132 vars */) = 0
write(1, "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBu"..., 4131
exit(0) = ?
The entire program is 1 write syscall that outputs the answer. I don't think it's theoretically possible to get faster than that.