Access the record fields in Ocaml

186 views
Skip to first unread message

oleg...@gmail.com

unread,
Dec 3, 2016, 1:34:56 PM12/3/16
to ocaml-core

I would like to write a function to compare if the children keys of the current node are less/greater than
their parent and I don't know how to do that.
I would really like to read more about lenses but I haven't found any material on the web.

The problem is , I can't write n.key >= n.left.key && n.key < n.right.key in the compare_children function;

Stack overflow link


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

Philippe Veber

unread,
Dec 3, 2016, 1:44:58 PM12/3/16
to ocaml...@googlegroups.com
Hi Oleg,

Indeed, [n.left] is an [avl_tree], not an [avl_tree_node]. As such it is not a record type and therefore has no [key] field. You have to do pattern matching on [n.left] and [n.right] to achieve what you want, like in:

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


Hope this helps,
  Philippe.


--
You received this message because you are subscribed to the Google Groups "ocaml-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

oleg...@gmail.com

unread,
Dec 3, 2016, 9:16:59 PM12/3/16
to ocaml-core
Thank you Philippe,
Do you know where I can read about the Ocaml lenses and optionals ?
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+...@googlegroups.com.

oleg...@gmail.com

unread,
Dec 3, 2016, 9:16:59 PM12/3/16
to ocaml-core
Thanks Philippe ,
One more question though :

This line of code from lens_right function :

set = fun node t -> {node with right = t}
it takes a node as an argument and then creates a brand new node with its right property being set to t ?


On Saturday, 3 December 2016 19:44:58 UTC+1, Philippe Veber wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+...@googlegroups.com.

Philippe Veber

unread,
Dec 4, 2016, 11:59:49 AM12/4/16
to ocaml...@googlegroups.com
2016-12-03 19:52 GMT+01:00 <oleg...@gmail.com>:
Thanks Philippe ,
One more question though :

This line of code from lens_right function :
set = fun node t -> {node with right = t}
it takes a node as an argument and then creates a brand new node with its right property being set to t ?

and all others properties equal to those of [node], yes.
 
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages