Files
book-exercises/Haskell-book/23/FizzBuzz.hs

30 lines
755 B
Haskell

module FizzBuzz where
import Control.Monad
import Control.Monad.Trans.State
fizzBuzz :: Integer -> String
fizzBuzz n
| n `mod` 15 == 0 = "FizzBuzz"
| n `mod` 5 == 0 = "Buzz"
| n `mod` 3 == 0 = "Fizz"
| otherwise = show n
fizzbuzzList :: [Integer] -> [String]
fizzbuzzList list = execState (mapM_ addResult list) []
addResult :: Integer -> State [String] ()
addResult n = do
xs <- get
let result = fizzBuzz n
put (result : xs)
fizzbuzzFromTo :: Integer -> Integer -> [String]
fizzbuzzFromTo from to = execState (mapM_ addResult (genList from [])) []
where genList from soFar
| from > to = soFar
| otherwise = genList (from + 1) (from : soFar)
main :: IO ()
main = mapM_ putStrLn $ fizzbuzzFromTo 1 100