Skip to content

Instantly share code, notes, and snippets.

@ar1a
Last active January 25, 2019 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ar1a/5400b32193a451b3e5d3bc550f84d81c to your computer and use it in GitHub Desktop.
Save ar1a/5400b32193a451b3e5d3bc550f84d81c to your computer and use it in GitHub Desktop.
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
@ar1a
Copy link
Author

ar1a commented Jan 24, 2019

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!

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