Skip to content

Instantly share code, notes, and snippets.

@jadlr
Last active April 6, 2016 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadlr/5981f66e6f5079e7e6c38c13ce0972af to your computer and use it in GitHub Desktop.
Save jadlr/5981f66e6f5079e7e6c38c13ce0972af to your computer and use it in GitHub Desktop.
fizzbuzz in haskell
{-# LANGUAGE ParallelListComp #-}
module FizzBuzz where
-- https://en.wikipedia.org/wiki/Fizz_buzz
fizzBuzz :: [String]
fizzBuzz = take 100 $ go 1
where go n
| n `mod` 15 == 0 = "fizzbuzz" : go (n + 1)
| n `mod` 3 == 0 = "fizz" : go (n + 1)
| n `mod` 5 == 0 = "buzz" : go (n + 1)
| otherwise = (show n) : go (n + 1)
fizzBuzz' :: [String]
fizzBuzz' = take 100 $ map f [1..]
where f n
| n `mod` 15 == 0 = "fizzbuzz"
| n `mod` 3 == 0 = "fizz"
| n `mod` 5 == 0 = "buzz"
| otherwise = (show n)
-- very cool impl fom @mittie (https://twitter.com/ostfale/status/717419614821593089)
fizzBuzz'' :: [String]
fizzBuzz'' = take 100 $ fb
where
fizzes = cycle ["", "", "fizz"]
buzzes = cycle ["", "", "", "", "buzz"]
pattern = zipWith (++) fizzes buzzes
numbers = map show [1..]
fb = zipWith max pattern numbers
-- variation of @mitties impl using the `ParallelListComp` extension
fizzBuzz''' :: [String]
fizzBuzz''' = take 100 $ [ max num (fizz ++ buzz) | fizz <- cycle ["", "", "fizz"]
| buzz <- cycle ["", "", "", "", "buzz"]
| num <- map show [1..]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment