summaryrefslogtreecommitdiff
path: root/Haskell-book/23/FizzBuzz.hs
blob: daee3f71ae3b18e11bada684d382d490431e20b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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