为什么在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).