Skip to content

Instantly share code, notes, and snippets.

@plaster
Created June 15, 2013 17:58
Show Gist options
  • Select an option

  • Save plaster/5788967 to your computer and use it in GitHub Desktop.

Select an option

Save plaster/5788967 to your computer and use it in GitHub Desktop.
Eitherの練習。リスト脳。
type FizzBuzz = Either Int String
hit :: String -> FizzBuzz -> FizzBuzz
hit s (Left _) = Right s
hit s (Right s') = Right $ s ++ s'
fizz :: [ FizzBuzz -> FizzBuzz ]
fizz = cycle [ id, id, hit "fizz" ]
buzz :: [ FizzBuzz -> FizzBuzz ]
buzz = cycle [ id, id, id, id, hit "buzz" ]
fizzbuzz :: [ FizzBuzz -> FizzBuzz ]
fizzbuzz = zipWith (.) fizz buzz
emit :: FizzBuzz -> String
emit (Left n) = show n
emit (Right s) = s
fizzbuzzlist :: [String]
fizzbuzzlist = map emit $ zipWith ($) fizzbuzz $ map Left [1..]
main :: IO ()
main = mapM_ putStrLn $ take 100 fizzbuzzlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment