Created
December 30, 2018 07:42
-
-
Save matthiasgoergens/91b4833b894aeac8f28ce389e8bf54c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
import Control.Monad.Trans.RWS.Strict | |
import Control.Monad | |
newtype Stitch = Stitch Int | |
deriving (Eq, Ord, Show, Num) | |
type Knitting a = RWS () [Stitch] Stitch a | |
emitRow :: Knitting () | |
emitRow = tell . pure =<< get | |
increase :: Stitch -> Knitting () | |
increase diff = modify (diff+) | |
decrease :: Stitch -> Knitting () | |
decrease diff = modify (subtract diff) | |
start :: Stitch -> Knitting () | |
start = put | |
pattern = do | |
start 100 | |
increase 2 | |
emitRow | |
increase 2 | |
now <- get | |
unless (now == 104) $ error "Doesn't match." | |
emitRow | |
decrease 4 | |
emitRow | |
-- runRWS | |
main = do | |
let (_, final, rows) = runRWS pattern () (Stitch 0) | |
mapM_ print rows |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment