Last active
January 25, 2019 00:07
-
-
Save ar1a/5400b32193a451b3e5d3bc550f84d81c to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Maybe (fromMaybe) | |
main :: IO () | |
main = print $ map fizzbuzz [1..110] | |
fizzbuzz :: Int -> String | |
fizzbuzz n = fromMaybe (show n) $ (fizz <> buzz <> diffie) n | |
fizz :: Int -> Maybe String | |
fizz = modStr 3 "Fizz" | |
buzz :: Int -> Maybe String | |
buzz = modStr 5 "Buzz" | |
diffie :: Int -> Maybe String | |
diffie = modStr 7 "Diffie" | |
modStr :: Int -> String -> Int -> Maybe String | |
modStr n str input | |
| input `mod` n == 0 = Just str | |
| otherwise = Nothing | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
instead of structuring it the classic way with guards and mod 3, mod 5, mod 15, this way allows for additional numbers/strings with ease - which is quite often a follow up question in interviews. here i've added 7 to show my cats name!