Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created November 14, 2016 20:11
Show Gist options
  • Save Dierk/c37941c9383b695b30209e34638f8d65 to your computer and use it in GitHub Desktop.
Save Dierk/c37941c9383b695b30209e34638f8d65 to your computer and use it in GitHub Desktop.
Monoidal Fizzbuzz in Purescript
module Main where
import Control.Monad.Eff.Console (log)
import Data.List.Lazy (take, zipWith, fromFoldable, cycle, iterate, foldr)
import Data.Monoid (mempty, (<>))
import Data.Maybe (Maybe(..), fromMaybe)
import Prelude ( show, map, ($), (+))
main = do
log $ join $ take 100 $ fizzbuzz
where
join = foldr (\x y -> x <> " " <> y) ""
nums = map show $ iterate (\x -> x+1) 1
fizzes = cycle $ fromFoldable [mempty, mempty, Just "fizz" ]
buzzes = cycle $ fromFoldable [mempty, mempty, mempty, mempty, Just "buzz" ]
pattern = zipWith (<>) fizzes buzzes
fizzbuzz = zipWith fromMaybe nums pattern
@Dierk
Copy link
Author

Dierk commented Nov 16, 2016

Thank you both! @mathiasverraes: I added your solution to the references section of https://dierk.gitbooks.io/fregegoodness/content/src/docs/asciidoc/fizzbuzz_monoid.html

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