[英]Error while composing two functions
I try to compose two function with type specifying. 我尝试用指定类型组成两个函数。
foo :: Num a => a -> a
foo a = a + 2
bar :: Num a => a -> a
bar a = a * 2
fooBarCompose :: (Num a, Num b, Num c) => (a -> b) -> (c -> a) -> c -> b
fooBarCompose f g = f . g
My module compiles, but in runtime when I invoke 我的模块可以编译,但是在运行时会调用
fooBarCompose bar foo
I get an error: 我收到一个错误:
No instance for (Show (b0 -> b0))
(maybe you haven't applied enough arguments to a function?)
arising from a use of ‘print’
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
Any ideas why I get this? 有什么想法我明白这个吗?
Any ideas why I get this?
有什么想法我明白这个吗?
You don't . 你不 。 Everything you've written works just fine.
您编写的所有内容都可以正常工作。 You can use
fooBarCompose bar foo
in any program you want. 您可以在所需的任何程序中使用
fooBarCompose bar foo
。
Only, if you try to evaluate it in GHCi, it has a problem: fooBarCompose bar foo
is a function. 仅当您尝试在GHCi中对其进行评估时,它会出现问题:
fooBarCompose bar foo
是一个函数。 How the heck is it supposed to show a function ? 应该如何显示功能 ? Display an exhaustive list of all possible inputs and corresponding results?
显示所有可能的输入和相应结果的详尽列表? Clearly not feasible.
显然不可行。 GHCi uses
print
under the hood, which simply invokes show
. GHCi在后台使用
print
,它仅调用show
。 And, well, because it's not possible to show a function, it gives you an error message saying exactly this. 而且,由于无法显示函数,因此会给您一条错误消息,说明正确的内容。
OTOH, the result of applying a function to any single value can easily be shown, eg OTOH,可以轻松显示将函数应用于任何单个值的结果,例如
> fooBarCompose bar foo 2 -- aka `bar . foo $ 2`
8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.