25 lines
686 B
Haskell
25 lines
686 B
Haskell
-- Do the game fizzbuzz (http://en.wikipedia.org/wiki/Fizz_buzz).
|
|
-- Return a string counting from 1 to the specified number.
|
|
-- Replace numbers dvisible by 3 with "fizz" and numbers divisible
|
|
-- by 5 with "buzz". If a number is divisible by both 3 and 5,
|
|
-- replace it with "fizzbuzz".
|
|
|
|
fb :: Int -> String
|
|
fb x | mod x 3 == 0 && mod x 5 == 0 = "fizzbuzz"
|
|
fb x | mod x 3 == 0 = "fizz"
|
|
fb x | mod x 5 == 0 = "buzz"
|
|
fb x = show x
|
|
|
|
fizzbuzz :: Int -> String
|
|
fizzbuzz n
|
|
| n < 0 = error "Non-negative numbers only"
|
|
| otherwise = unwords $ map fb [1..n]
|
|
|
|
main :: IO ()
|
|
main = do
|
|
print (fizzbuzz 1)
|
|
print (fizzbuzz 7)
|
|
print $ fizzbuzz 99
|
|
print $ fizzbuzz 0
|
|
print $ fizzbuzz (-2)
|