-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenvironment.ml
67 lines (60 loc) · 1.58 KB
/
environment.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
63
64
65
66
67
open Sexp
let append lst o =
let rec loop ptr =
match (cdr ptr) with
Cons (_) -> loop (cdr ptr)
| _ ->
match ptr with
Cons (c) ->
c.cdr <- cons o Null
| _ -> invalid_arg "Append needs a cdr"
in
loop lst
let rec replace_atom sexp rep =
match sexp with
Cons (_) ->
begin
let lst = (cons (replace_atom (car sexp) rep) Null) in
let rec loop s =
match s with
Cons(_) ->
begin
append lst (replace_atom (car s) rep) ;
loop (cdr s)
end
| _ -> ()
in
loop (cdr sexp);
lst
end
| _ ->
let rec loop tmp =
match tmp with
Cons (_) ->
begin
let item = (car tmp) in
let atom = (car item) in
let replacement = (car (cdr item)) in
if (name atom) = (name sexp) then
replacement
else
loop (cdr tmp)
end
| _ -> sexp
in
loop rep
let interleave c1 c2 =
let lst = cons (cons (car c1) (cons (car c2) Null)) Null in
let c1' = cdr c1 in
let c2' = cdr c2 in
let rec loop a b =
match a with
Cons (_) ->
begin
append lst (cons (car a) (cons (car b) Null)) ;
loop (cdr a) (cdr b)
end
| _ -> ()
in
loop c1' c2' ;
lst