patternMinor
F#zzBuzz: Learning the easy way
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
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
0Solution
First off, I don't see a real need for these two variables:
To me, they just seem a little unnecessary. In fact, writing out the expression
Next, I'd highly recommend that you get into the habit of using Match Expressions rather than plain old
Would become a Match Expression, like this:
After all these improvements to your code, I ended up with the following, working code:
let valueMod3 = value % 3
let valueMod5 = value % 5To 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)
0Code Snippets
let valueMod3 = value % 3
let valueMod5 = value % 5if 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)
0Context
StackExchange Code Review Q#112934, answer score: 6
Revisions (0)
No revisions yet.