Last active
August 29, 2015 13:56
-
-
Save nbouscal/9291933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-} | |
import Control.Arrow | |
import Data.Monoid | |
type FizzBuzz = Either String Int | |
showFB :: FizzBuzz -> String | |
showFB (Left x) = x | |
showFB (Right x) = show x | |
instance Monoid FizzBuzz where | |
mempty = Right 0 | |
mappend (Left x) (Left y) = Left (x ++ y) | |
mappend x@(Left _) _ = x | |
mappend _ y@(Left _) = y | |
mappend (Right m) (Right n) = Right (max m n) | |
predfb :: String -> Int -> Int -> FizzBuzz | |
predfb s m n | n `mod` m == 0 = Left s | |
| otherwise = Right n | |
fizz = predfb "fizz" 3 | |
buzz = predfb "buzz" 5 | |
fizzbuzz = [1..100] >>= return . (fizz &&& buzz >>> uncurry mappend >>> showFB) | |
main :: IO () | |
main = putStr $ show $ fizzbuzz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment