Skip to content

Instantly share code, notes, and snippets.

@kazu-yamamoto
Created October 9, 2012 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazu-yamamoto/3857500 to your computer and use it in GitHub Desktop.
Save kazu-yamamoto/3857500 to your computer and use it in GitHub Desktop.
Monoid FizzBuzz
import Data.Monoid
data FizzBuzz = N | I Int | S String
instance Monoid FizzBuzz where
mempty = N
N `mappend` N = N
N `mappend` I i = S $ show i
N `mappend` S x = S x
I i `mappend` N = S $ show i
I _ `mappend` S x = S x
S x `mappend` I _ = S x
S x `mappend` S y = S $ x <> y
S x `mappend` N = S x
I _ `mappend` I _ = error "FizzBuzz"
fizzbuzz :: [String]
fizzbuzz = map (\(S x) -> x) fbs
where
fizz = cycle [N, N, S "Fizz"]
buzz = cycle [N, N, N, N, S "Buzz"]
is = map I [1..]
fbs = zipWith (<>) is $ zipWith (<>) fizz buzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment