HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

F#zzBuzz: Learning the easy way

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
zzbuzzthewaylearningeasy

Problem

So I made a quick programme in F# to do FizzBuzz, I'm trying to learn more languages, so I figured I'd do something functional for a change. (Haskell is also on the list.)

FizzBuzz is pretty self-explanatory: every third value print Fizz, every fifth print Buzz (possible both if it's a multiple of 3 and 5).

[]
let main argv = 
    let fizzBuzzSequence = seq {
        for value in 1..100 do
            let valueMod3 = value % 3
            let valueMod5 = value % 5

            if valueMod3 = 0 && valueMod5 = 0 then yield "FizzBuzz"
            else if valueMod3 = 0 && valueMod5 <> 0 then yield "Fizz"
            else if valueMod3 <> 0 && valueMod5 = 0 then yield "Buzz"
            else yield value.ToString()
    }
    for value in fizzBuzzSequence do printfn "%s" value
    0

Solution

First off, I don't see a real need for these two variables:

let valueMod3 = value % 3
let valueMod5 = value % 5


To me, they just seem a little unnecessary. In fact, writing out the expression value % 3 or value % 5 takes the exact same amount of keystrokes as writing valueMod3 or valueMod5.

Next, I'd highly recommend that you get into the habit of using Match Expressions rather than plain old if statements, as it's more functional, and will give you more benefits, like Pattern matching, over if statements. This means that this small chunk of if statements:

if valueMod3 = 0 && valueMod5 = 0 then yield "FizzBuzz"
else if valueMod3 = 0 && valueMod5 <> 0 then yield "Fizz"
else if valueMod3 <> 0 && valueMod5 = 0 then yield "Buzz"
else yield value.ToString()


Would become a Match Expression, like this:

match n with
| n when n % 3 = 0 && n % 5 = 0 -> yield "FizzBuzz"
| n when n % 3 = 0 -> yield "Fizz"
| n when n % 5 = 0 -> yield "Buzz"
| _ -> yield n.ToString()


After all these improvements to your code, I ended up with the following, working code:

let fizzbuzzSequence = seq { 
    for n in 1 .. 100 do
        match n with
        | n when n % 3 = 0 && n % 5 = 0 -> yield "FizzBuzz"
        | n when n % 3 = 0 -> yield "Fizz"
        | n when n % 5 = 0 -> yield "Buzz"
        | _ -> yield n.ToString()
}

[]
let main argv = 
    for item in fizzbuzzSequence do
        System.Console.WriteLine(item)

    0

Code Snippets

let valueMod3 = value % 3
let valueMod5 = value % 5
if valueMod3 = 0 && valueMod5 = 0 then yield "FizzBuzz"
else if valueMod3 = 0 && valueMod5 <> 0 then yield "Fizz"
else if valueMod3 <> 0 && valueMod5 = 0 then yield "Buzz"
else yield value.ToString()
match n with
| n when n % 3 = 0 && n % 5 = 0 -> yield "FizzBuzz"
| n when n % 3 = 0 -> yield "Fizz"
| n when n % 5 = 0 -> yield "Buzz"
| _ -> yield n.ToString()
let fizzbuzzSequence = seq { 
    for n in 1 .. 100 do
        match n with
        | n when n % 3 = 0 && n % 5 = 0 -> yield "FizzBuzz"
        | n when n % 3 = 0 -> yield "Fizz"
        | n when n % 5 = 0 -> yield "Buzz"
        | _ -> yield n.ToString()
}

[<EntryPoint>]
let main argv = 
    for item in fizzbuzzSequence do
        System.Console.WriteLine(item)

    0

Context

StackExchange Code Review Q#112934, answer score: 6

Revisions (0)

No revisions yet.