Fmap print value doesn't print anything

Why the following doesn't print anything:

λ> fmap print (pure 2)

While something like this works:

λ> fmap id (pure 2)
2
+1
source share
2 answers

Perform the following types:

fmap print (pure 2) :: Applicative f => f (IO ())
fmap id (pure 2)    :: (Num b, Applicative f) => f b

Allows to replace fwith IO:

fmap print (pure 2) :: IO (IO ())      -- #1
fmap id (pure 2)    :: (Num b) => IO b -- #2

Now you can clearly see that # 2 is an action with a numerical result, while # 1 is an action with another action as a result.

In addition, GHCi has the following interactive evaluation rules :

2.4. Interactive evaluation at the command line

When entering an expression at the prompt, GHCi immediately evaluates and prints the result:

2.4.1. Command Line I / O

GHCi . - IO a a, GHCi IO-.

, GHCi -, ( ):

  • - Show.

  • ().

IO a Show, -.

+6

( ).

fmap print (pure 2) >>= id

.

fmap print (pure 2) :: Applicative f => f (IO ())

fmap print (pure 2) >>= id            -- #1

( "" ),

fmap id (pure 2)                      -- #2

# 1

# 2 fmap ,

(

fmap id (pure 2) :: (Num b, Applicative f) => f b
+2

All Articles