Skip to content

Instantly share code, notes, and snippets.

@ab9rf
Last active April 6, 2021 01:27
Show Gist options
  • Save ab9rf/1d1ff99b5307d7994fa3 to your computer and use it in GitHub Desktop.
Save ab9rf/1d1ff99b5307d7994fa3 to your computer and use it in GitHub Desktop.
Silly haskell version of that FizzBuzz problem. Written because I was bored.
-- new and improved!
import Control.Applicative ((<|>))
import Data.Maybe (catMaybes)
nfilter n k = cycle $ take n $ (Just k) : (repeat Nothing)
joinf Nothing Nothing = Nothing
joinf Nothing (Just a) = Just a
joinf (Just a) Nothing = Just a
joinf (Just a) (Just b) = Just $ a ++ b
fizzF = nfilter 3 "Fizz"
buzzF = nfilter 5 "Buzz"
fizzbuzzF = zipWith joinf fizzF buzzF
fizzbuzz = catMaybes $ tail $ zipWith (<|>) fizzbuzzF $ map (Just . show) [0..]
main = mapM_ putStrLn (take 100 fizzbuzz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment