Skip to content

Instantly share code, notes, and snippets.

@mjeff7

mjeff7/Main.hs

Created Mar 23, 2018
Embed
What would you like to do?
fizzbuzz in Haskell
module Main where
import Data.Monoid ((<>))
import Data.Maybe (fromMaybe)
import Control.Applicative (liftA2)
main :: IO ()
main = print $ take 100 $ fizzBuzzIntString <$> [1..]
fizzBuzzIntString :: Int -> String
fizzBuzzIntString = fromMaybe <$> show <*> fizzString <> buzzString
-- Alternatively:
-- fizzBuzzIntString = fromMaybe . show <*> fizzString <> buzzString
-- fizzBuzzIntString = liftA2 fromMaybe show $ fizzString <> buzzString
fizzString :: Int -> Maybe String
fizzString n = if n `mod` 3 == 0 then Just "fizz" else Nothing
buzzString :: Int -> Maybe String
buzzString n = if n `mod` 5 == 0 then Just "buzz" else Nothing
@mrallen1

This comment has been minimized.

Copy link

@mrallen1 mrallen1 commented Mar 24, 2018

Cool!

@rickowens

This comment has been minimized.

Copy link

@rickowens rickowens commented Mar 29, 2018

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment