Skip to main content

FizzBuzz

I've always found the FizzBuzz problem to be quite uninteresting, but I guess that it is quite good to rule out candidates in an early stage. You quickly notice if they can express themselves in code or not.

The problem is simple. Write out the number 1 .. 100 and switch out each number divisible by 3 with "fizz" and every number divisible by 5 with "buzz. Any number divisible with both 3 and 5 should be replace by "fizz buzz".

Here's a trivial solution in F#. Enjoy.

// System.String.Join(" ", fizzbuzz 1 |> Seq.take 100)
let rec fizzbuzz x = seq {
        yield
            match x % 3, x % 5 with
            | 0, 0 -> "fizz buzz"
            | 0, _ -> "fizz"
            | _, 0 -> "buzz"
            | _, _ -> x.ToString()
        yield! fizzbuzz (x + 1)
    }

comments powered by Disqus