由于使用“打印”而没有(Show(IO())的实例) [英] No instance for (Show (IO ())) arising from a use of ‘print’

查看:122
本文介绍了由于使用“打印”而没有(Show(IO())的实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  let ls = [putChar'x',putChar'y '] 

然后如果这样做:

 头部ls 

输出很明显 x <

  tail ls 

我得到这个错误:


由于使用'print'而导致没有用于(Show(IO())的实例

在交互式GHCi命令中输入:print it


为什么?不应输出 y [putChar'y']

解决方案

解释此行为 here

head ls 的类型为 IO(),所以GCHi执行动作并且不打印结果()



相反, tail ls 的类型为 [IO()] 。由于这不是 IO 操作,GCHi会尝试使用 print 显示它,该类型为:

  print ::显示a => a  - > IO()

但是因为没有显示实例为 [IO()] 你会得到错误。如果您想评估您可以使用的操作 sequence_

  sequence_ (tail ls)


Just for learning I did this on my console WinGHCi:

let ls = [putChar 'x',putChar 'y']

then if do:

head ls

the output is obviusly x (obviusly in the sense that I undestand why)

else if I do:

tail ls

I get this error:

No instance for (Show (IO ())) arising from a use of ‘print’

In a stmt of an interactive GHCi command: print it

Why? Shouldn't output y or [putChar 'y']?

解决方案

This behaviour is explained here.

head ls has type IO () so GCHi executes the action and doesn't print the result ().

In contrast, tail ls has type [IO ()]. Since this is not an IO action, GCHi attempts to display it using print which has type:

print :: Show a => a -> IO ()

however since there is no Show instance for [IO ()] you get the error. If you want to evaluate the actions you can use sequence_:

sequence_ (tail ls)

这篇关于由于使用“打印”而没有(Show(IO())的实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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