-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tree.ml
40 lines (31 loc) · 843 Bytes
/
Tree.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(**
* Definitions that do not assume/preserve ordering.
*)
id ∷ Tree α → Tree α
id x = x
singleton ∷ α → Tree α
singleton x = node leaf x leaf
iter ∷ Tree α → Tree α
iter t = match t with
| node l x r → node (iter l) x (iter r)
left ∷ Tree α → Tree α
left t = match t with
| node l _ _ → l
right ∷ Tree α → Tree α
right t = match t with
| node _ _ r → r
flip ∷ Tree α → Tree α
flip t = match t with
| node l x r → node r x l
clone ∷ (α ⨯ Tree α) → Tree α
clone x t = node t x t
empty ∷ Tree α → Bool
empty t = match t with
| leaf → true
| node r x l → false
contains ∷ Eq α ⇒ (α ⨯ Tree α) → Bool
contains x t = match t with
| leaf → false
| node l y r → if x == y
then true
else (Bool.or (contains x l) (contains x r))