ocaml - 访问 Ocaml 中的记录字段

标签 ocaml

问题是,我无法在 compare_children 函数中写入 n.key >= n.left.key && n.key < n.right.key ;

我想像OOP中那样写node.left.right.left...

我真的很想了解有关镜头的更多信息,但我在网上没有找到任何 Material 。

type avl_tree =
    Node of avl_tree_node
  | Leaf
and avl_tree_node = { 
  key : int;
  balance : int;
  left : avl_tree;
  right : avl_tree;
}

type subtree_lens = {
  get : avl_tree_node -> avl_tree;
  set : avl_tree_node -> avl_tree -> avl_tree_node
}

let lens_right = {
  get = (fun node -> node.right);
  set = fun node t -> {node with right = t}
}

let lens_left = {
  get = (fun node -> node.left);
  set = fun node t -> {node with left = t}
}

let compare_children nd =
  match nd with
  | Leaf -> true
  | Node n -> n.key >= n.left.key && n.key < n.right.key

最佳答案

看待这个问题的一种方法是,您不能编写 n.left.key 因为 n.left 可能是 Leaf .

如果您想保留类型定义,则必须将 LeafNode 作为单独的情况处理:

let compare_children nd =
    match nd with
    | Leaf -> true
    | Node { left = Leaf } -> (* Leaf case, fill in... *) false
    | Node { right = Leaf } -> (* Other leaf case, fill in... *) false
    | Node { left = Node ln; right = Node rn; key = k } ->
        k >= ln.key && k < rn.key

更新

OCaml 表达式可以如下所示:{ x with f = v }。表达式x 的类型必须是包含名为f 的字段的记录类型。该表达式计算结果为一条记录,其字段与 x 的字段相同,但 f 字段的值为 v。事实上,with 之后可以有任意数量的字段。

要访问 nd 的字段,您可以使模式更加明确:

let compare_children nd =
    match nd with
    | Leaf -> true
    | Node { left = Leaf; right = Leaf } -> true
    | Node { left = Leaf; right = Node rn; key = k } -> k < rn.key
    | Node { left = Node ln; right = Leaf; key = k } -> k >= ln.key
    | Node { left = Node ln; right = Node rn; key = k } ->
        k >= ln.key && k < rn.key

请注意,我只是猜测该函数应该返回什么。这只是您可能想要使用的模式的示例。

关于ocaml - 访问 Ocaml 中的记录字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40950935/

相关文章:

c - 为什么 OCaml 中没有函数头?

Ocaml的命名参数

utf-8 - OCaml websocket "Invalid UTF8 data"

types - 为什么这个类型前面有一个加号?

functional-programming - "Break"在 OCaml 中不合时宜?

function - 什么时候可以调用带有标签参数而不带标签的函数?

ocaml - 带有可选和可变字段的记录

c - 为什么 OCaml 中的 while 循环不能按预期工作?

ocaml - OCaml 中的引用透明性

arrays - OCaml 中的数组操作