|
|
|
|
|
by throwaway81523
1300 days ago
|
|
I don't like that solution very much because of the 4 cases, or generally n*2 cases if there are n tokens. My favorite Haskell solution uses monoids (specificaly the Monoid instance of Maybe String) and is quite natural in Haskell but would take some explanation for non-Haskellers. My version looks like the below. I didn't originate the approach but I like this spelling. It won't make total sense to non-Haskellers though. {-# LANGUAGE MonadComprehensions #-}
import Data.Maybe
import Data.Monoid
fizz :: Int -> String
fizz n = fromMaybe (show n) $ f 3 "Fizz" <> f 5 "Buzz"
where
f :: Int -> String -> Maybe String
f d msg = [msg | n`rem`d == 0]
main = mapM_ putStrLn [fizz n | n <- [1..100]]
This extends in an obvious way to FizzBuzzHissHowl, lets the Monoid instance manage concatenating the special tokens together while remembering if any of the rules have matched, and uses a monad comprehension on the Maybe monad to cleanly lift the token into the Maybe monad if the number is a multiple of whatever. The type annotation on f is added to make it a little bit clearer what is going on.The use of monoids and the monad comprehension may be slightly flashy, but the use of Maybe to remember whether a rule has triggered is second nature in Haskell. |
|