Skip to content

Instantly share code, notes, and snippets.

@aymanosman
Last active August 29, 2015 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aymanosman/ebaa7aebb87d8d00400b to your computer and use it in GitHub Desktop.
Save aymanosman/ebaa7aebb87d8d00400b to your computer and use it in GitHub Desktop.
fizzbuzz.hs
import Data.Maybe
import Data.Monoid
import Control.Applicative
-- Mostly stolen from an answer found posted on reddit.
-- This formulation achieves a high level of modularity and clarity.
-- It is modular with respect to adding more numbers to fizz and buzz against,
-- and captures the idea of `show`ing the number by default elegantly.
t m s n = if mod n m == 0 then Just s else Nothing
-- instance Applicative ((->) a) where (<*>) f g x = f x (g x)
fz = fromMaybe . show <*> (t 3 "fizz" <> t 5 "buzz" <> t 7 "hiss" <> t 11 "howl")
main = mapM_ (putStrLn . fz) [1..100]
-- fz = fromMaybe . show <*> (\t -> t 3 "fizz" <> t 5 "buzz" <> t 7 "hiss" <> t 11 "howl") (\m s n -> s <$ guard (mod n m == 0))
-- fz n = (\t -> show n `t` (["fizz" | mod n 3 == 0] ++ ["buzz" | mod n 5 == 0])) (\d x -> case x of [] -> d; _ -> concat x)
fromList xs = fromMaybe . show <*> (mconcat $ map (uncurry t) xs)
fromList [(3, "Fizz"), (5, "Buzz"), (7, "Hiss")] (3*5*7) -- => "FizzBuzzHiss"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment