Skip to content

Instantly share code, notes, and snippets.

@nbouscal
Last active August 29, 2015 13:56
Show Gist options
  • Save nbouscal/9291933 to your computer and use it in GitHub Desktop.
Save nbouscal/9291933 to your computer and use it in GitHub Desktop.
{-# 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