进入do块的fmap因打印错误而失败

问题描述 投票:1回答:2

我试图理解为什么我用do-block编写的函数不能被重写为在列表上fmap类似的lambda表达式。

我有以下内容:

-- This works
test1 x = do 
        let m = T.pack $ show x
        T.putStrLn m

test1 1

产生

1

-- This fails
fmap (\x -> do 
              let m = T.pack $ show x
              T.putStrLn m
              ) [1..10]

-- And this also fails
fmap (\x -> do 
             T.putStrLn $ T.pack $ show x
                ) [1..10]

有错误:

<interactive>:1:1: error:
    • No instance for (Show (IO ())) arising from a use of ‘print’
    • In a stmt of an interactive GHCi command: print it

我的putStrLn在工作和非工作之间是一致的。进口是一样的。我需要打印的show-pack-putstrln舞蹈在工作和非工作之间也是一致的。

印刷品的使用在工作和非工作之间发生了什么变化?

更新1

-- I was also surprised that this fails
fmap (T.putStrLn $ T.pack $ show) [1..10]
-- it seemed as similar as possible to the test1 function but mapped.

<interactive>:1:7: error:
    • Couldn't match expected type ‘Integer -> b’ with actual type ‘IO ()’
    • In the first argument of ‘fmap’, namely ‘(T.putStrLn $ pack $ show)’
      In the expression: fmap (T.putStrLn $ pack $ show) [1 .. 10]
      In an equation for ‘it’: it = fmap (T.putStrLn $ pack $ show) [1 .. 10]
    • Relevant bindings include it :: [b] (bound at <interactive>:1:1)
<interactive>:1:29: error:
    • Couldn't match type ‘() -> String’ with ‘String’
      Expected type: String
        Actual type: () -> String
    • Probable cause: ‘show’ is applied to too few arguments
      In the second argument of ‘($)’, namely ‘show’
      In the second argument of ‘($)’, namely ‘pack $ show’
      In the first argument of ‘fmap’, namely ‘(T.putStrLn $ pack $ show)’

更新2

-- This lambda returns x of the same type as \x
-- even while incidentally printing along the way
fmap (\x -> do 
              let m = T.pack $ show x
              T.putStrLn $ m
              return x
              ) [1..10]

但也失败了:

<interactive>:1:1: error:
    • No instance for (Show (IO Integer)) arising from a use of ‘print’
    • In a stmt of an interactive GHCi command: print it
haskell monads io-monad do-notation
2个回答
1
投票

fmap f [1..10]的类型是[T],其中Tf的返回类型。

在你的情况下,T = IO (),所以完整表达式的类型是[IO ()]

无法打印IO操作,因此当您尝试打印该列表时,GHCi会抱怨。您可能想要使用像sequence_ (fmap f [1..10])之类的东西来运行这些操作而不是打印它们。

或者,考虑放弃fmap,而是使用类似的东西

import Data.Foldable (for_)

main = do
   putStrLn "hello"
   for_ [1..10] $ \i -> do
      putStrLn "in the loop"
      print (i*2)
   putStrLn "out of the loop"

1
投票

你写了:

但是当我将lambda的返回类型更改为与更新2中的x相同的x时...

不,不。你没有。 lambda函数返回其最后一个表达式的值。你的lambda函数只有一个表达式 - 整个do { ... }块定义了一个值,即lambda函数的返回值。不是xreturn属于do,而不是lambda表达。我们更容易看到我们是否使用显式分隔符来编写它,如

fmap (\x -> do {
              let m = T.pack $ show x ;
              T.putStrLn $ m ;
              return x
              } ) [1..10]

do块作为一个整体具有与其每个行语句相同的monadic类型。

其中一个是putStrLn ...,其类型是IO ()。所以你的lambda函数为某些IO t返回t

由于return xtx的类型。我们有return :: Monad m => t -> m t,所以与m ~ IO它是return :: t -> IO t

x来自参数列表Num t => [t],所以总的来说你有

Num t => fmap (fx :: t -> IO t) (xs :: [t]) :: [IO t]

要么

           xs :: [t]
        fx    ::  t  ->  IO t
   ----------------------------
   fmap fx xs ::        [IO t]
© www.soinside.com 2019 - 2024. All rights reserved.