## Posted By

rtperson on 06/01/11

## Versions (?)

`-- file: Fizz.hs-- a Haskell implementation of the fizzbuzz problem ns = [0..100] :: [Int] fizz :: Int -> Stringfizz n = if (n `mod` 3) == 0 then "fizz" else "" buzz :: Int -> Stringbuzz n = if (n `mod` 5) == 0 then "buzz" else "" -- a generalized version. It takes the index and the value to divide against, and -- returns the message if n is evenly divisible by xfluff :: Int -> Int -> String -> Stringfluff n x message = if (n `mod` x) == 0 then message else "" -- a purely functional implementation.fizzBuzz :: [String]fizzBuzz = zipWith (++) (map fizz ns) (map buzz ns) -- another purely functional version. Very easy to remember. threes :: [String]threes = cycle ["", "", "Fizz"] fives :: [String]fives = cycle ["", "", "", "", "Buzz"] fizzBuzzCycle :: [String]fizzBuzzCycle = zipWith (++) threes fives -- List comprehensions, anyone?boomBang :: [String]boomBang =     [ if x `mod` 15 == 0        then "boombang"         else if x `mod` 3 == 0                then "boom"                 else if x `mod` 5 == 0                    then "bang"                     else show x       | x <- ns] -- the answer your recruiter is probably looking for-- (if your recruiter has enough doubts about your  -- programming ability that he/she busts out -- fizzbuzz on your butt)main :: IO () main = printAll \$ map fizz' [1..100]         where             printAll [] = return ()            printAll (x:xs) = putStrLn x >> printAll xs fizz' :: Int -> Stringfizz' n    | n `mod` 15 == 0  = "fizzbuzz"    | n `mod` 3 == 0   = "fizz"    | n `mod` 5 == 0   = "buzz"    | otherwise          = show n -- or, to get rid of the explicit recursion in the main routine main2 :: IO ()main2 = printAll \$ map fizz' ns        where             printAll xs = foldr ((>>) . putStrLn) (return ()) xs`