Last active
April 6, 2016 09:15
-
-
Save jadlr/5981f66e6f5079e7e6c38c13ce0972af to your computer and use it in GitHub Desktop.
fizzbuzz in haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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