OCaml:首先应用第二个参数(高阶函数)

use*_*812 3 ocaml functional-programming readability higher-order-functions

我定义了一个这样的高阶函数:

val func : int -> string -> unit
Run Code Online (Sandbox Code Playgroud)

我想以两种方式使用这个功能:

other_func (func 5)
some_other_func (fun x -> func x "abc")
Run Code Online (Sandbox Code Playgroud)

即,通过使用已定义的参数之一创建函数.但是,第二种用法比第一种用法更简洁和可读.是否有更可读的方法来传递第二个参数来创建另一个函数?

Jef*_*eld 7

在Haskell中,有一个功能flip.您可以自己定义:

let flip f x y = f y x
Run Code Online (Sandbox Code Playgroud)

然后你可以说:

other_func (func 5)
third_func (flip func "abc")
Run Code Online (Sandbox Code Playgroud)

翻转在Jane Street Core中定义为Fn.flip.它在OCaml电池中定义为BatPervasives.flip.(换句话说,每个人都同意这是一个有用的功能.)