为什么在OCaml中更喜欢使用curring而不是元组参数? [英] Why prefer currying to tuple arguments in OCaml?

查看:94
本文介绍了为什么在OCaml中更喜欢使用curring而不是元组参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

骆驼简介"

请注意,在Caml中,最好将Curried函数定义用于多参数函数,而不是元组.

Note, in Caml it is better to use Curried function definitions for multiple-argument functions, not tuples.

'a -> 'b -> 'c调用约定与'a * 'b -> 'c进行比较时.

when comparing 'a -> 'b -> 'c calling conventions to 'a * 'b -> 'c.

在使用SML/NJ时,我习惯于将元组类型同时用于输入和输出:('a * 'b) -> ('c * 'd),因此使用元组来表示多个输入似乎与我表示多个输出的方式对称.

When working with SML/NJ I got used to using tuple types for both input and output : ('a * 'b) -> ('c * 'd) so using tuples to express multiple inputs seems symmetric with the way I express multiple outputs.

为什么建议对元组参数的OCaml函数声明使用curry?仅仅是允许进行分期评估/部分评估而带来的更大灵活性,还是从OCaml编译器的实现细节中获得了其他好处?

Why is currying recommended for OCaml function declarations over tuple arguments? Is it just the greater flexibility that comes with allowing currying/partial evaluation, or is there some other benefit that derives from implementation details of the OCaml compiler?

推荐答案

是的,主要是符号上的方便和进行部分应用程序的灵活性.在OCaml中,咖喱函数是惯用的,并且编译器可能会比元组函数更好地优化它们(而SML编译器通常会针对元组进行优化).

Yes, it is mainly the notational convenience and the flexibility to do partial application. Curried functions are idiomatic in OCaml, and the compiler is likely to optimise them somewhat better than tupled functions (whereas SML compilers typically optimise for tuples).

纠缠的优点是您提到的参数/结果对称性(在编写函数时特别有用),或者可能是符号上的熟悉(至少对于来自非函数世界的人而言).

The pros of tupling are the argument/result symmetry you mention (which is especially useful when composing functions) and perhaps the notational familiarity (at least for people coming from the non-functional world).

这篇关于为什么在OCaml中更喜欢使用curring而不是元组参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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