4 ms·
No where near compact (or possibly obtuse?) enough for a Haskell version, surely? Here's one I found elsewhere on the net sometime ago: (~>) :: (Integral a
by pja 9y ago
No where near compact (or possibly obtuse?) enough for a Haskell version, surely? Here's one I found elsewhere on the net sometime ago:
(~>) :: (Integral a) => a -> String -> a -> Maybe String
(n ~> str) i = str <$ guard (i `mod` n == 0)
fizzbuzz = 3~>"Fizz" <> 5~>"Buzz"
main = mapM_ putStrLn $ catMaybes $ fizzbuzz <$> [1..100]
At least it generalises nicely to arbitrary fizzbuzz numbers! (edit: as noted below this version as written fails to print the non fizzbuzz digits. An exercise for the reader :) )
- KirinDave 9y agoTBF, I wrote this article 5 years ago. Monad Comprehensions were in fashion at the time. Now every dang person wants to write an arrow with a tilde in it and exclaim, 'Naturality!' ;) Gotta love the use of guard in this. Maybe I should revisit. I suspect there is also a clever way to do this applicatively, but haven't really noodled with it to see if it lines up. Any chance to use applicativeDo!
- wz1000 9y agofizzbuzzbazz = mconcat [ 3 ~> "Fizz" , 5 ~> "Buzz" , 7 ~> "Bazz" ] Also, I believe you should have something like main = mapM_ putStrLn $ map (fromMaybe . show <*> fizzbuzz) [1..100] to print out the number when it isn't matched by any of the rules.
- pja 9y agoPoint! I did have something like that in the original version, but was experimenting with other approaches and lost the digits somewhere along the way.
- paulajohnson 9y agoFor those who don't speak Haskell, its worth noting that "<>" is the monoid concatenation operator. The "~>" operator returns a function type "a -> Maybe String". A function type is a monoid if its return type is a monoid, so this works.