您如何使用F#或任何功能语言来处理第二(或第三,第四,...)参数? [英] How do you curry the 2nd (or 3rd, 4th, ...) parameter in F# or any functional language?

查看:60
本文介绍了您如何使用F#或任何功能语言来处理第二(或第三,第四,...)参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用F#,看看如何使用currying将第一个参数预加载到函数中.但是,如何使用第二,第三或其他任何参数呢?命名参数可以简化此过程吗?是否还有其他命名参数的功能语言,或通过其他方式使currying变得对参数顺序无动于衷?

解决方案

通常,您只使用lambda:

fun x y z -> f x y 42

是类似于'f'的函数,但第三个参数绑定为42.

您还可以使用组合器(例如在评论中提到Haskell的翻转"的人)对参数进行重新排序,但是有时我会感到困惑.

请注意,大多数咖喱函数的编写都是为了使最有可能被部分应用的参数排在首位.

F#为方法命名了参数(不是让函数绑定的函数值),但是名称适用于成对的"参数.命名的咖喱参数没有多大意义;如果我有两个参数的咖喱函数"f",我希望给定

let g = f
let h x y = f x y

然后'g'或'h'可以替代'f',但是'named'参数使其不一定成立.也就是说,命名参数"与语言设计的其他方面交互作用较差,我个人并不知道命名参数"与一流的咖喱函数值"良好交互的良好设计. /p>

I'm just starting up with F# and see how you can use currying to pre-load the 1st parameter to a function. But how would one do it with the 2nd, 3rd, or whatever other parameter? Would named parameters to make this easier? Are there any other functional languages that have named parameters or some other way to make currying indifferent to parameter-order?

解决方案

Typically you just use a lambda:

fun x y z -> f x y 42

is a function like 'f' but with the third parameter bound to 42.

You can also use combinators (like someone mentioned Haskell's "flip" in a comment), which reorder arguments, but I sometimes find that confusing.

Note that most curried functions are written so that the argument-most-likely-to-be-partially-applied comes first.

F# has named parameters for methods (not let-bound function values), but the names apply to 'tupled' parameters. Named curried parameters do not make much sense; if I have a two-argument curried function 'f', I would expect that given

let g = f
let h x y = f x y

then 'g' or 'h' would be substitutable for 'f', but 'named' parameters make this not necessarily true. That is to say, 'named parameters' can interact poorly with other aspects of the language design, and I personally don't know of a good design offhand for 'named parameters' that interacts well with 'first class curried function values'.

这篇关于您如何使用F#或任何功能语言来处理第二(或第三,第四,...)参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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