fmap进入do块失败,并显示打印错误 [英] fmap into a do block fails with a print error

查看:80
本文介绍了fmap进入do块失败,并显示打印错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to understand why a function I have written with a do-block can't be rewritten to fmap a similar lambda expression over a list.

我有以下内容:

-- 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舞步在正常工作和无效工作之间也是一致的.

My putStrLn is consistent between the working and the non-working. The imports are the same. My show-pack-putstrln dance required to print is also consistent between the working and the non-working.

正在使用的打印纸和正在使用的打印纸正在改变,这是怎么回事?

What is happening that the use of print is changing between the working and non-working?

-- 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

推荐答案

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

The type of fmap f [1..10] is [T] where T is the return type of f.

在您的情况下为T = IO (),因此完整表达式的类型为[IO ()].

In your case, T = IO (), so the type of the full expression is [IO ()].

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

IO actions can not be printed, so GHCi complains when you try to print that list. You might want to run those actions instead of printing them, using something like sequence_ (fmap f [1..10]).

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

Alternatively, consider ditching fmap and instead using something like

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"

这篇关于fmap进入do块失败,并显示打印错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆