Skip to content

Commit

Permalink
More code.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Sep 7, 2024
1 parent 8bc07be commit a8ca286
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions haskell/readerstate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ instance Monad (Reader env) where

-- ANCHOR_END: Monad_Reader

runReader :: env -> Reader env a -> a
runReader env (Reader f) = f env

ask :: Reader env env
ask = Reader $ \env -> env

local :: (env -> env) -> Reader env a -> Reader env a
local f (Reader g) = Reader $ \env -> g (f env)

data Tree
= Leaf Int
| Inner Tree Tree
deriving (Show)

incLeaves :: Tree -> Reader Int Tree
incLeaves (Leaf x) = do
depth <- ask
pure $ Leaf $ x + depth
incLeaves (Inner l r) = do
l' <- local (+ 1) $ incLeaves l
r' <- local (+ 1) $ incLeaves r
pure $ Inner l' r'

-- ANCHOR: State
newtype State s a = State (s -> (a, s))

Expand Down

0 comments on commit a8ca286

Please sign in to comment.