Skip to content

Instantly share code, notes, and snippets.

@Rembane
Created May 21, 2015 10:12
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 Rembane/d34cc87ba1ad69f30310 to your computer and use it in GitHub Desktop.
Save Rembane/d34cc87ba1ad69f30310 to your computer and use it in GitHub Desktop.
Another FizzBuzz. This time with lots of recursion.
module Main where
continue :: ([a] -> [b]) -> [a] -> [b]
continue _ [] = []
continue f (x:xs) = f (x:xs)
f1, f2, f3, b1, b2, b3, b4, b5 :: [Int] -> [(Bool, String)]
f1 (x:xs) = (False, show x):continue f2 xs
f2 (x:xs) = (False, show x):continue f3 xs
f3 (x:xs) = (True, "Fizz"):continue f1 xs
b1 (x:xs) = (False, show x):continue b2 xs
b2 (x:xs) = (False, show x):continue b3 xs
b3 (x:xs) = (False, show x):continue b4 xs
b4 (x:xs) = (False, show x):continue b5 xs
b5 (x:xs) = (True, "Buzz"):continue b1 xs
run :: [Int] -> [String]
run xs = zipWith go (f1 xs) (b1 xs)
where
go (False, s1) (False, _) = s1
go (True, s1) (False, _) = s1
go (False, _) (True, s2) = s2
go (True, s1) (True, s2) = s1 ++ s2
main :: IO ()
main = mapM_ putStrLn $ run [1..100]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment