Skip to content

Instantly share code, notes, and snippets.

@BBBSnowball
Last active March 10, 2018 03:17
Show Gist options
  • Save BBBSnowball/edc4ad63764b2a82acd3963c75387239 to your computer and use it in GitHub Desktop.
Save BBBSnowball/edc4ad63764b2a82acd3963c75387239 to your computer and use it in GitHub Desktop.
walktree :: Tree -> Counter Tree
walktree (Node left right) = do
left' <- walktree left
right' <- walktree right
return (Node left' right')
--walktree (Node left right) =
-- walktree left >>= \left' ->
-- walktree right >>= \right' ->
-- return (Node left' right')
walktree (NormalLeaf x) = return x
walktree (VariableLeaf x) = do
cnt <- nextCounter
return (VariableLeaf cnt)
nextCounter :: Counter Integer
let nextCounter = do
cnt <- get
put (cnt+1)
return cnt
type Counter t = cnt -> (t, Integer)
--type Counter t = State Integer t
instance Monad Counter where
return x = (x, 0)
-- (>>=) = bind
(>>=) :: Counter t -> (t -> Counter u) -> Counter u
(>>=) m f = \cnt ->
let (result1, cnt1) = m cnt
(f result1) cnt1
get :: Counter Integer
get = \cnt -> (cnt, cnt)
put :: Integer -> Counter ()
put cnt = \oldcnt -> ((), cnt)
runCounter :: Counter t -> t
runCounter m = let (result, cnt) = m 0 in result
runCounter (walktree mytree) :: Tree
--main :: IO ()
--main = do
-- something
-- main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment