Skip to content

Instantly share code, notes, and snippets.

@matthiasgoergens
Created December 30, 2018 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthiasgoergens/91b4833b894aeac8f28ce389e8bf54c5 to your computer and use it in GitHub Desktop.
Save matthiasgoergens/91b4833b894aeac8f28ce389e8bf54c5 to your computer and use it in GitHub Desktop.
{-# 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