了解 syb 样板消除
在 http:// 中给出的示例中web.archive.org/web/20080622204226/http://www.cs.vu.nl/boilerplate/
-- Increase salary by percentage
increase :: Float -> Company -> Company
increase k = everywhere (mkT (incS k))
-- "interesting" code for increase
incS :: Float -> Salary -> Salary
incS k (S s) = S (s * (1+k))
为什么增加函数编译时没有绑定其类型签名中提到的第一家公司的任何内容。
是不是类似于分配给部分函数?为什么要这样做呢?
In the example given in http://web.archive.org/web/20080622204226/http://www.cs.vu.nl/boilerplate/
-- Increase salary by percentage
increase :: Float -> Company -> Company
increase k = everywhere (mkT (incS k))
-- "interesting" code for increase
incS :: Float -> Salary -> Salary
incS k (S s) = S (s * (1+k))
how come increase function compiles without binding anything for the first Company mentioned in its type signature.
Is it something like assigning to a partial function? Why is it done like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这与部分应用是相同的概念。该行是更短(但可以说不太清楚)的等价物,
因为
everywhere
接受两个参数,但只给出一个参数,everywhere (mkT (incS k)) 的类型是
公司 ->公司
。因为这正是increase k
对每个 Float k 返回的结果,所以increase
的结果类型是Float ->公司->公司
。Yes, it's the same concept as partial application. The line is a shorter (but arguably less clear) equivalent of
As
everywhere
takes two parameters but is only given one, the type ofeverywhere (mkT (incS k))
isCompany -> Company
. Because this is exactly whatincrease k
returns for each Float k, the resulting type ofincrease
isFloat -> Company -> Company
.