Hi
I’m learning OCaml module syntax currently and confusing why this syntax is not allowed and why OCaml says “unbound value pp”.
Code:
1 module Tree = struct
2 type 'a tree =
3 Br of 'a * 'a tree * 'a tree
4 | Lf
5
6 let create v =
7 Br(v,Lf,Lf)
8
9 let link_left dst src =
10 match src with
11 | Br(v,_,r) -> Br(v,dst,r)
12 | _ -> Lf
13
14 let link_right dst src =
15 match src with
16 | Br(v,l,_) -> Br(v,l,dst)
17 | _ -> Lf
18
19 let rec size = function
20 | Br(v,l,r) -> 1 + max (size l) (size r)
21 | Lf -> 0
22
23 let rec pp f = function
24 | Br(v,l,r) -> (f v); (pp f l); (pp f r)
25 | Lf -> ()
26 end
27
28
29 (* test *)
30 let _ =
31 let a = create 1 in
32 let b = create 2 in
33 let c = create 3 in
34 let tree = link_left b a |> link_right c in
35 let fmt = Printf.printf "%d\n" in
36 pp fmt tree;;
~
What I want to learn the semantics of syntax in the above code is three points:
-
31, 32, 33 : let .. = create n
How can I callcreate
function? create is inside moduleTree
but this code works. I don’t know why. -
31, 32, 33 : let .. = create n
If I changecreate
function toTree.create
, generates stuff what I don’t understand. the return value isTree.tree
not'a tree
. Why it has happened? questions 1 and 2 make me difficult to understand module syntax. -
36: pp fmt tree;;
create, link_left, link_right, and size are no problems when I call them but, the functionpp
occurred error when I call it.
Thank you.