Created
March 23, 2018 21:31
-
-
Save mjeff7/a5ffcb411268c42f9351f5f1824046f5 to your computer and use it in GitHub Desktop.
fizzbuzz in Haskell
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool!