3 ms·
which is nice with the laziness because you only eval what you need, e.g. fizzle :: Int -> String fizzle n | fizz && baz = "Fizz Baz!" -- if this
by internet_points 1y ago
which is nice with the laziness because you only eval what you need, e.g.
fizzle :: Int -> String
fizzle n
| fizz && baz = "Fizz Baz!" -- if this branch is taken, buzz is never evaluated
| fizz && buzz = "Fizz Buzz!"
| fizz = "Fizz!"
| buzz = "Buzz!"
| otherwise = show n
where
fizz = n `mod` 3 == 0
buzz = n `mod` 5 == 0
baz = n `mod` 7 == 0
- throwaway81523 1y agoThat doesn't handle all the cases: what about fizz buzz bazz? Added: here's my version, which maybe could be cleaned up further. The spaces and exclamation point make the problem trickier. {-# LANGUAGE MonadComprehensions #-} import Data.Maybe (catMaybes) import Data.List (intercalate) fizz :: Int -> String fizz n = case zub of [] -> show n otherwise -> intercalate " " zub ++ "!" where f :: Int -> String -> Maybe String f d msg = [msg | n`rem`d == 0] zub = catMaybes . map (uncurry f) $ [(3,"Fizz"), (5,"Buzz"), (7, "Bazz")] main = mapM_ putStrLn [fizz n | n <- [1..110]]
- throwaway81523 1y agoimport Data.List (intercalate) fizzle :: Int -> String fizzle n = let ws = [(3,"Fizz"), (5,"Buzz"), (7,"Bazz")] a = [s | (d,s) <- ws, n`rem`d == 0] in case a of [] -> show n otherwise -> intercalate " " a ++ "!" main = mapM_ (putStrLn . fizzle) [1..110]