fizzbuzz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Control.Monad (guard)
import Control.Applicative ((<|>))
import Data.Maybe (fromJust)
import Data.Monoid (mappend)

allOf = foldl mappend Nothing
oneOf = foldl (<|>)   Nothing

rules = [
    (3, "Fizz"),
    (5, "Buzz")
  ]

main = mapM_ putStrLn $ map fizzbuzz [1..100]
  where
    fizzbuzz :: Int -> String
    fizzbuzz i = fromJust $ oneOf [
      allOf $ map (\(d, s) -> guard (i `mod` d == 0) >> Just s) rules,
      Just $ show i]
14:8: Error: Use .
Found:
mapM_ putStrLn $ map fizzbuzz [1 .. 100]
Why not:
mapM_ (putStrLn . fizzbuzz) [1 .. 100]