在ghci中获得IO Monad的结果

Kev*_*ith 3 io haskell

鉴于:

Prelude> let x = return 100 :: IO Int
Run Code Online (Sandbox Code Playgroud)

试图评估x返回其包装值.

Prelude> x
100
Run Code Online (Sandbox Code Playgroud)

但是,我无法通过它获得它的价值show.

Prelude> show x

<interactive>:4:1:
    No instance for (Show (IO Int)) arising from a use of ‘show’
    In the expression: show x
    In an equation for ‘it’: it = show x
Run Code Online (Sandbox Code Playgroud)

当我输入xghci 时会发生什么?

sep*_*p2k 8

如果IO t在GHCi中输入类型的表达式,它会将其解包并打印结果值.也就是说,如果你输入ioExp,GHCi会执行val <- ioExp; print val(而如果你输入一个非IO表达式exp,GHCi就会执行print exp).