-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HigherOrder.ml
62 lines (53 loc) · 1.48 KB
/
HigherOrder.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(*
(**
* NOTE: All of this file is commented, since it contains syntax that cannot be
* parsed with current implementations, such as higher order functions and
* trees containing booleans.
* If you want to try it out, remove the outermost comment (in the first and
* last line of this file).
*)
and ∷ Tree Bool → Bool
and t =
match t with
| leaf → true
| node l x r →
if and l
then if x then and r
else false
else false;
or ∷ Tree Bool → Bool
or t =
match t with
| leaf → false
| node l x r →
if or l then true
else if x then true
else or r;
any ∷ (α → Bool) ⨯ Tree α → Bool
any f t = or (map f t);
all ∷ (α → Bool) ⨯ Tree α → Bool
all f t = and (map f t);
map ∷ (α → β) ⨯ Tree α → Tree β
map f t =
match t with
| node l x r → (node (map f l) (f x) (map f r));
fold ∷ (β * α → β) * β ⨯ Tree α → β
fold f z t =
match t with
| leaf → z
| node l x r → f (f (fold f z l) x) (fold f z t);
zipWith ∷ (a * β → γ) ⨯ Tree α ⨯ Tree β → Tree γ
zipWith f t u =
match t with
| node l x r →
match u with
| node v y w → let l' = zipWith f l v in
let r' = zipWith f r w in
(node l' (f x y) r');
n1 ∷ (α → Tree β) * a * β ⨯ Tree β → Tree β
n1 f x y z =
let l = f x in
let r = (node l y z)
in
(node l y z);
*)