Give a definition of the function
fmap :: (a->b) -> IO a -> IO b
the effect of which is to transform an interaction by applying the function to its result. you should define it using the do construct.
how should I define the fmap? I have no idea about it?
could someone help me with that?
Thanks~!
It looks like homework or something so I will give you enough hint so that you can work rest of the details yourself.
actionis asIOaction andfis a function fromatoband hence typea -> b.If you are familiar with monadic bind
>>=which has type (simplified forIOmonad)Now if you look at
It means perform the
IOaction which returns an output (sayoutof typea) and pass the output tofwhich is of typea -> IO band hencef outis of typeIO b.If you look at the second function called
returnwhich has type (again simlified forIOmonad)It takes a pure value of type
aand gives anIOaction of typeIO a.Now lets look back to
fmap.which performs the
IOaction and then runsfon the output of the action and then converts the output to anotherIOaction of typeIO b. ThereforeNow comes the syntactic sugar of
donotation. Which is just to write bind>>=in another way.In
donotation you can get the output of an action bySo bind just reduces to
I think now you will be able to convert the definition of fmap to do construct.