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

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

问题描述

使用多个模式匹配,即使没有点,也无法使用不同数量的参数!

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

foo True b = b + 2
foo _ = id

例如,

无效.但是

doesn't work for example. But

foo True = (+2)
foo _ = id

确实.有时我们只能在函数的一部分中使用无点,所以...

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 p1 1 …p1 k match1

x pn 1 …pn k 匹配

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 = \ x 1 …x k -> case(x 1 ,…,x k

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

(p11,…,p1k)match1

(pn1,…,pnk)matchn
其中x i 是新标识符.

(p11, …, p1k) match1

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

(重点是我的)

虽然函数定义与lambda&案例表达,不一定像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; dr

不允许这样做的选择只是风格问题. –八月

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

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

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