在 Ocaml 中找到 **,但不是求幂

问题描述

在一本关于逻辑的书 (https://www.cl.cam.ac.uk/~jrh13/atp/OCaml/real.ml) 中,我找到了这样的代码

let integer_qelim =
   simplify ** evalc **
   lift_qelim linform (cnnf posineq ** evalc) cooper;;

我之前见过 ** ,但它是用于求幂的,而在这里我认为它不用于那个,因为数据类型不是数字。我会说它是函数组合器的王者,但不知道。

我认为这本书是为 3.06 版本编写的,但是 4 的更新代码 (https://github.com/newca12/ocaml-atp) 保持了这一点,所以 ** 仍然以我不理解的方式使用。>

解决方法

在 OCaml 中,您可以将任何行为绑定到运算符,例如,

let ( ** ) x y = print_endline x; print_endline y

这样 "hello" ** "world" 就会打印

hello
world

在您引用的代码中,(**) 运算符绑定到函数组合:

let ( ** ) = fun f g x -> f(g x)
,

这是在 lib.ml 中定义的效用函数:

let ( ** ) = fun f g x -> f(g x);;

它是一个组合运算符,在其他示例中通常称为 compose

你可以这样使用它:

let a x = x^"a" in
let b x = x^"b" in
let c x = x^"c" in
let foo = a ** b ** c in
foo "input-";;
- : string = "input-cba"

你可以把它写成

let foo x = a (b (c x))

let foo x = a @@ b @@ c x

let foo x = c x |> b |> a

也一样。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...