Haskell函数组合的一些错误和困难 [英] Some errors and difficulties with Haskell function composition
问题描述
$ b
< interactive>:17:1:error:
?约束中的非类型变量参数:Num(a - > c)
(使用FlexibleContexts来允许)
?当检查推断类型
it :: forall c a。 (Num(a→c),Num c)=> a - > c
函数组合运算符出现错误。为什么它不起作用? fx
工程, gx
工程,甚至 f(gx)
works,但 fg x
不起作用。 代码 f。 g x
不起作用,因为它被解析为 f。 (g x)
。也就是说,首先将 g
应用于 x
,然后尝试获取<$ c $结果是 gx
。
为了达到这个目的,可以用圆括号(f。g)x
包围组合物,或使用具有最低优先级的 $
运算符的所有操作符,因此可以用来分隔事物: f。 g $ x
。
f x = x + 3
g x = x * 3
<interactive>:17:1: error:
? Non type-variable argument in the constraint: Num (a -> c)
(Use FlexibleContexts to permit this)
? When checking the inferred type
it :: forall c a. (Num (a -> c), Num c) => a -> c
I'm getting an error with function composition operator. Why does it not work? f x
works, g x
works, and even f(g x)
works, but f.g x
doesn't work.
The code f . g x
doesn't work because it gets parsed as f . (g x)
. That is, at first g
is applied to x
, and then you try to get the composition of f
with the result of g x
.
To make this work, you can surround the composition with parentheses (f . g) x
, or use the $
operator, which has the lowest priority of all operators and thus can be used to separate things: f . g $ x
.
这篇关于Haskell函数组合的一些错误和困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!