PFP Laboratory 8

From Marek Běhálek Wiki
Jump to navigation Jump to search

Monads

  • Create a type
type SimpleState s a = s -> (s, a)

Implement the monadic functions return and bind

Show solution
  • 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

Show solution
  • Define a function readInt', so that following code will be valid:
add :: State [Int] Int
add = do x<-readInt'
         y<-readInt'
         return (x+y)
Show solution