Skip to content

Instantly share code, notes, and snippets.

@matthewleon
Last active May 10, 2017 22:28
Show Gist options
  • Save matthewleon/77951008507fb6d0904c3c9bf29eade8 to your computer and use it in GitHub Desktop.
Save matthewleon/77951008507fb6d0904c3c9bf29eade8 to your computer and use it in GitHub Desktop.
purescript fizzbuzz DSL
-- based on "FizzBuzz in Haskell by Embedding a Domain-Specific Language"
-- by Maciej Piróg
-- https://themonadreader.files.wordpress.com/2014/04/fizzbuzz.pdf
-- and "skiphaltprint" by Mathias Verraes
-- https://github.com/mathiasverraes/skiphaltprint
module Main where
import Prelude
import Control.Monad.Eff (Eff, forE)
import Control.Monad.Eff.Console (CONSOLE, log)
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = forE 1 101 \i -> log $ fizzbuzz i
fizzbuzz :: Int -> String
fizzbuzz n = rules id $ show n
where
test :: Int -> String -> (String -> String) -> (String -> String)
test d s f | n `mod` d == 0 = const $ s <> f ""
| otherwise = f
rules = test 3 "fizz" <<< test 5 "buzz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment