Haskell 中的参数数量和无点数 [英] Number of arguments and point-free in Haskell

查看:20
本文介绍了Haskell 中的参数数量和无点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用多个模式匹配,不同数量的参数是不可能的,即使是无点!

With multiple pattern-matching, different numbers of arguments are impossible, even with point-free!

foo True b = b + 2
foo _ = id

例如不起作用.但是

foo True = (+2)
foo _ = id

确实如此.有时我们只能在函数的一部分使用point-free,所以...

does. Sometimes we can use point-free only in one part of the function, so...

为什么?GHC 是不是太难了?:'(

Why? Is it too hard for GHC? :'(

推荐答案

为什么?GHC 是不是太难了?

Why? Is it too hard for GHC?

.对于 GHC 来说一点也不难.实际上,这是 Haskell 报告的错.

No. It is not at all too hard for GHC. Actually, this is the fault of the Haskell Report.

请参阅:Haskell 报告 2010 > 声明和绑定 > 函数绑定

函数绑定将变量绑定到函数值.变量 x 的函数绑定的一般形式是:

A function binding binds a variable to a function value. The general form of a function binding for variable x is:

x p11 ... p1k match1
……
x pn1 ... pnk 匹配n

x p11 … p1k match1

x pn1 … pnk matchn

[...等等等等...]

[...blah blah...]

翻译:函数的一般绑定形式在语义上等同于等式(即简单的模式绑定):

Translation: The general binding form for functions is semantically equivalent to the equation (i.e. simple pattern binding):

x = \ x1 ... xk -> case (x1, ..., xk) 的

x = \ x1 … xk -> case (x1, …, xk) of

(p11, …, p1k) match1
……
(pn1, ..., pnk) matchn
其中 xi 是新标识符.

(p11, …, p1k) match1

(pn1, …, pnk) matchn
where the xi are new identifiers.

(强调我的)

虽然函数定义在语义上等价到一个 lambda &case 表达式,它们不一定像 Mihai 建议的那样编译.

While function definitions are semantically equivalent to a lambda & case expression, they are not necessarily compiled that way, as Mihai suggests.

问题是,Haskell 报告定义函数声明,使得它们必须在等式左侧具有相同数量的输入.k 在第 1 行和第 n 行函数声明行(以及其间的所有行)都保持相同,这一事实清楚地表明了这一点.这个是限制的原因;与 GHC 的实施细节无关.

The thing is, the Haskell report defines function declarations such that they must have the same number of inputs on the left-hand side of the equation. This is made clear by the fact that k remains the same on both the 1st and the nth function declaration lines (and by implication, all lines in-between). This is the reason for the restriction; it has nothing to do with GHC's implementation details.

tl;博士

选择不允许它只是风格问题.– 八月

The choice not to allow it is just a matter of style. – augustss

这篇关于Haskell 中的参数数量和无点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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