How to code Fizzbuzz in F#

Posted by Russell on Stack Overflow See other posts from Stack Overflow or by Russell
Published on 2010-03-11T05:15:29Z Indexed on 2010/03/11 5:18 UTC
Read the original article Hit count: 301

Filed under:
|

I am currently learning F# and have tried (an extremely) simple example of FizzBuzz.

This is my initial attempt:

for x in 1..100 do 
    if x % 3 = 0 && x % 5 = 0 then printfn "FizzBuzz"  
    elif x % 3 = 0 then printfn "Fizz"
    elif x % 5 = 0 then printfn "Buzz"
    else printfn "%d" x

What solutions could be more elegant/simple/better (explaining why) using F# to solve this problem?

Note: The FizzBuzz problem is going through the numbers 1 to 100 and every multiple of 3 prints Fizz, every multiple of 5 prints Buzz, every multiple of both 3 AND 5 prints FizzBuzz. Otherwise, simple the number is displayed.

Thanks :)

© Stack Overflow or respective owner

Related posts about F#

Related posts about fizzbuzz