2020 年,您可以使用Bool.to_int
. 来自 Bool 库文档:
val to_int : bool -> int
如果 b 为假,则 b 为 0;如果 b 为真,则 b 为 1。
来源:https ://caml.inria.fr/pub/docs/manual-ocaml/libref/Bool.html
这是一个可能的实现:
let int_of_bool b = if b then 1 else 0
Run Code Online (Sandbox Code Playgroud)
图书馆OCaml Batteries Included to_int
在其BatBool模块中具有功能.