OCaml 中的 `and` 关键字是什么意思?
What does the `and` keyword mean in OCaml?
我对 OCaml 中的 and
关键字感到困惑。翻阅this code,我看到
type env = {
(* fields for a local environment described here *)
}
and genv {
(* fields for a global environment here *)
}
然后 later,
let rec debug stack env (r, ty) = (* a function definition *)
and debugl stack env x = (* another function definition *)
这是怎么回事? and
关键字是否只是复制最后一个 type
、let
或 let rec
语句?是否有 and rec
语句之类的东西?为什么我要使用 and
而不是只键入 let
或 type
,从而使我的代码不那么容易重构?还有什么我应该知道的吗?
and
关键字用于避免多个 let
(第一个示例,我从来没有为此使用它,但为什么不使用它)或用于类型、函数、模块的相互递归定义。 .
如您在第二个示例中所见:
let rec debug stack env (r, ty) =
...
| Tunresolved tyl -> o "intersect("; debugl stack env tyl; o ")"
...
and debugl stack env x =
...
| [x] -> debug stack env x
...
debug
调用 debugl
,反之亦然。所以 and
允许这样做。
[编辑] 没有给出一个合适的例子让我很困扰,所以这是一个你经常会看到的例子:
let rec is_even x =
if x = 0 then true else is_odd (x - 1)
and is_odd x =
if x = 0 then false else is_even (x - 1)
(* second version *)
let rec is_even x =
x = 0 || is_odd (x - 1)
and is_odd x =
x <> 0 && is_even (x - 1)
(你可以找到这个例子here)
对于相互递归的类型,很难找到一个配置,但是在 this wikipedia page 之后,我们将 trees
和 forests
定义为
type 'a tree = Empty | Node of 'a * 'a forest
and 'a forest = Nil | Cons of 'a tree * 'a forest
例如,由空树、标记为 a
的单例树和标记为 b
和 c
的双节点树组成的森林将表示为:
let f1 = Cons (Empty, (* Empty tree *)
Cons (Node ('a', (* Singleton tree *)
Nil), (* End of the first tree *)
Cons (Node ('b', (* Tree composed by 'b'... *)
Cons (Node ('c', (* and 'c' *)
Nil),
Nil)
),
Nil (* End ot the second tree *)
)
)
);;
大小函数(计算森林中的节点数)为:
let rec size_tree = function
| Empty -> 0
| Node (_, f) -> 1 + size_forest f
and size_forest = function
| Nil -> 0
| Cons (t, f) -> size_tree t + size_forest f
然后我们得到
# size_forest f1;;
- : int = 3
我对 OCaml 中的 and
关键字感到困惑。翻阅this code,我看到
type env = {
(* fields for a local environment described here *)
}
and genv {
(* fields for a global environment here *)
}
然后 later,
let rec debug stack env (r, ty) = (* a function definition *)
and debugl stack env x = (* another function definition *)
这是怎么回事? and
关键字是否只是复制最后一个 type
、let
或 let rec
语句?是否有 and rec
语句之类的东西?为什么我要使用 and
而不是只键入 let
或 type
,从而使我的代码不那么容易重构?还有什么我应该知道的吗?
and
关键字用于避免多个 let
(第一个示例,我从来没有为此使用它,但为什么不使用它)或用于类型、函数、模块的相互递归定义。 .
如您在第二个示例中所见:
let rec debug stack env (r, ty) =
...
| Tunresolved tyl -> o "intersect("; debugl stack env tyl; o ")"
...
and debugl stack env x =
...
| [x] -> debug stack env x
...
debug
调用 debugl
,反之亦然。所以 and
允许这样做。
[编辑] 没有给出一个合适的例子让我很困扰,所以这是一个你经常会看到的例子:
let rec is_even x =
if x = 0 then true else is_odd (x - 1)
and is_odd x =
if x = 0 then false else is_even (x - 1)
(* second version *)
let rec is_even x =
x = 0 || is_odd (x - 1)
and is_odd x =
x <> 0 && is_even (x - 1)
(你可以找到这个例子here)
对于相互递归的类型,很难找到一个配置,但是在 this wikipedia page 之后,我们将 trees
和 forests
定义为
type 'a tree = Empty | Node of 'a * 'a forest
and 'a forest = Nil | Cons of 'a tree * 'a forest
例如,由空树、标记为 a
的单例树和标记为 b
和 c
的双节点树组成的森林将表示为:
let f1 = Cons (Empty, (* Empty tree *)
Cons (Node ('a', (* Singleton tree *)
Nil), (* End of the first tree *)
Cons (Node ('b', (* Tree composed by 'b'... *)
Cons (Node ('c', (* and 'c' *)
Nil),
Nil)
),
Nil (* End ot the second tree *)
)
)
);;
大小函数(计算森林中的节点数)为:
let rec size_tree = function
| Empty -> 0
| Node (_, f) -> 1 + size_forest f
and size_forest = function
| Nil -> 0
| Cons (t, f) -> size_tree t + size_forest f
然后我们得到
# size_forest f1;;
- : int = 3