3 ms·
The solution in Haskell is quite clean, I believe. fizzBuzz n | n `mod` 15 == 0 = "FizzBuzz" | n `mod` 3 == 0 = "Fizz" | n `mod` 5 == 0 = "
by qubitcoder 12y ago
The solution in Haskell is quite clean, I believe.
fizzBuzz n
| n `mod` 15 == 0 = "FizzBuzz"
| n `mod` 3 == 0 = "Fizz"
| n `mod` 5 == 0 = "Buzz"
| otherwise = show n
main = mapM_ (print . fizzBuzz) [1..100]
I agree with you about generalizing pattern matching for less simple cases. Your example brought to mind view patterns, about which Oliver O'Charles had a nice writeup recently [1]. Nifty little extension.
[1] https://ocharles.org.uk/blog/posts/2014-12-02-view-patterns.html https://ocharles.org.uk/blog/posts/2014-12-02-view-patterns....
- ghuntley 12y agoUsing F# pattern matching: let buzzer number = match number with | i when i % 3 = 0 && i % 5 = 0 -> "FizzBuzz" | i when i % 3 = 0 -> "Fizz" | i when i % 5 = 0 -> "Buzz" | i -> (sprintf "%i" i) for i = 1 to 100 do printfn "%s" (buzzer i)