Why does the following doesn't print anything:
λ> fmap print (pure 2)
Whereas something like this works:
λ> fmap id (pure 2)
2
Why does the following doesn't print anything:
λ> fmap print (pure 2)
Whereas something like this works:
λ> fmap id (pure 2)
2
Your expression return a computation (no perform it).
You want
with that, we get your computation and run it inside monad.
Look your types
Note
is very different (but same "visual" result) than
with #1 we take a computation and run it inside monad printing a number
with #2 we take a number and get value through
fmap
returning a number(looking types again
Follow the types:
Lets replace
f
withIO
:Now you can clearly see that #2 is an action with a numeric result, whereas #1 is an action with another action as a result.
Also, GHCi has the following rules about interactive evaluation:
Since
IO a
is not an instance ofShow
, it won't print the result of the IO action.