为什么没有功能的Show实例? [英] Why is there no Show instance for functions?
本文介绍了为什么没有功能的Show实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道Show函数用于将值转换为字符串,但为什么can not函数类型用于show?
Prelude> (\ x - > x * 3)
< interactive>:7:1:
(Show(a0 - > a0))
没有实例使用`print'
可能的修正:为(Show(a0 - > a0))添加一个实例声明
在一个交互式GHCi命令中:print it
Prelude>
解决方案
这不是他们不能的,而是那里通常不是一个很好的理由。
但是,如果你愿意,你绝对可以:
前奏> :{
Prelude |实例显示(a - > b)其中
Prelude |显示_ =一个功能。
Prelude | :}
前奏>打印(\ x - > x + 7)
一个函数。
前奏>打印(\ a b c - > a + b + c)
一个函数。
如果您想要 show
函数的文本表示,好吧 - 你不能这么做。与Ruby,JS等元编程语言不同,Haskell对其内部的代码知之甚少。
Just a quick conceptual question, I am currently trying to learn and understand Haskell better.
I know the Show function is used to convert values to strings, but why can't function types be used with show?
Prelude> (\x -> x*3)
<interactive>:7:1:
No instance for (Show (a0 -> a0))
arising from a use of `print'
Possible fix: add an instance declaration for (Show (a0 -> a0))
In a stmt of an interactive GHCi command: print it
Prelude>
解决方案
It's not that they can't, but that there's not usually a good reason to.
But if you'd like, you definitely can:
Prelude> :{
Prelude| instance Show (a -> b) where
Prelude| show _ = "A function."
Prelude| :}
Prelude> print (\x -> x + 7)
A function.
Prelude> print (\a b c -> a + b + c)
A function.
If you'd like to show
the textual representation of the function, well - you can't do that. Unlike metaprogramming languages like Ruby, JS, etc, Haskell code very little knowledge of its own internals.
这篇关于为什么没有功能的Show实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文