PFP Laboratory 8
Jump to navigation
Jump to search
Monads
- Create a type
type SimpleState s a = s -> (s, a)
Implement the monadic functions return
and bind
- Define a new type and a function:
type ListInput a = SimpleState [Int] a
readInt :: ListInput Int
readInt stateList = (tail stateList, head stateList)
Use the previously defined functions to bind actions readInt
.
- Consider you have a type:
newtype State s a = State { runState :: s -> (s, a) }
Make this type the instance of Monad
- Define a function
readInt'
, so that following code will be valid:
add :: State [Int] Int
add = do x<-readInt'
y<-readInt'
return (x+y)