I do not understand why this sample of code does not work and in RWH book it works:
module Monads where
import Data.Maybe
import Control.Monad
amap=[("a",1),("bb",2)]
bmap=[(1,100),(2,200)]
final=[(100,1000),(200,2000)]
f::String->IO (Maybe Int)
f par=do
a<-lookup par amap
b<-lookup a bmap
lookup b final
It does not work and i do not understand how it could work since the last line returns a Maybe something
and not a IO something
.
I have also tried changing the last line to :
return (lookup b final)
which to my reasoning should be perfect ( lookup returns a Maybe Int
and then wrap it with return
)
I get the following error in when using return
* Couldn't match type `Maybe' with `IO'
Expected type: IO Integer
Actual type: Maybe Integer
* In a stmt of a 'do' block: b <- lookup a bmap
In the expression:
do a <- lookup par amap
b <- lookup a bmap
return (lookup b final)
In an equation for `f':
f par
= do a <- lookup par amap
b <- lookup a bmap
return (lookup b final)
|
11 | b<-lookup a bmap
| ^^^^^^^^^^^^^