OCaml:将 bool 转换/转换为 int

OCaml: Converting / casting bool to int

如何将 ocaml 布尔值转换为整数? (没有 int_of_bool 函数。)

这是一种可能的实施方式:

let int_of_bool b = if b then 1 else 0

库 OCaml Batteries Included 在其 BatBool module 中有一个函数 to_int

2020年可以使用Bool.to_int。 来自 Bool 库文档:

val to_int : bool -> int
b is 0 if b is false and 1 if b is true.

来源:https://caml.inria.fr/pub/docs/manual-ocaml/libref/Bool.html