-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse.ml
165 lines (120 loc) · 3.07 KB
/
reverse.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
exception Empty
;;
type 'a linked_list =
| None
| Node of
{ value : 'a ref
; next : 'a linked_list ref
}
;;
let linked_list : 'a -> 'a linked_list =
fun x ->
Node { value = ref x
; next = ref None
}
;;
let get_value : 'a linked_list -> 'a =
fun l ->
match l with
| None -> raise Empty
| Node r -> !(r.value)
;;
let set_value : 'a -> 'a linked_list -> unit =
fun value node ->
match node with
| None -> ()
| Node r -> r.value := value
;;
let get_next : 'a linked_list -> 'a linked_list =
fun l ->
match l with
| None -> raise Empty
| Node r -> !(r.next)
;;
let set_next : 'a linked_list -> 'a linked_list -> unit =
fun n1 n2 ->
match n1 with
| None -> ()
| Node r -> r.next := n2
;;
let reverse : 'a linked_list -> 'a linked_list =
fun l ->
let rec reverse' : 'a linked_list -> 'a linked_list -> 'a linked_list =
fun n1 n2 ->
match n2 with
| None -> n1
| _ ->
let n3 = get_next n2
in
( set_next n2 n1 ; reverse' n2 n3 )
in
match l with
| None -> None
| _ -> reverse' None l
;;
let rec iterate : ('a -> unit) -> 'a linked_list -> unit =
fun f l ->
match l with
| None -> ()
| _ -> ( f (get_value l) ; iterate f (get_next l) )
;;
let print_list : ('a -> string) -> 'a linked_list -> unit =
fun to_string l ->
iterate (fun n -> print_string ((to_string n) ^ " ")) l
;;
let print_a_list_of_strings : string linked_list -> unit =
fun l -> print_list (fun s -> s) l
;;
let print_a_list_of_ints : int linked_list -> unit =
fun l -> print_list (fun i -> string_of_int i) l
;;
let main =
let l1 = linked_list 1
and l2 = linked_list 2
and l3 = linked_list 3
and l4 = linked_list 4
and l5 = linked_list 5
in
let lists =
[ ("l1", l1) ; ("l2", l2)
; ("l3", l3) ; ("l4", l4)
; ("l5", l5)
]
in
( print_newline ()
; print_string ">>> Before linking the lists\n"
; List.iter (fun (s, l) ->
print_string (s ^ " : " )
; print_a_list_of_ints l
; print_newline ()
)
lists
; set_next l1 l2
; set_next l2 l3
; set_next l3 l4
; set_next l4 l5
; print_newline ()
; print_string ">>> After linking and before reversing the linked list\n"
; List.iter (fun (s, l) ->
print_string (s ^ " : " )
; print_a_list_of_ints l
; print_newline ()
)
lists
; print_newline ()
; print_string " The evaluation of `reverse l1´ computes the reversed\n\
\ list of l1 and the final result is equal to l5,\n\
\ the new head of the linked list.\n\n"
; let _ = reverse l1 in ()
; print_string ">>> After reversing the linked list\n"
; List.iter (fun (s, l) ->
print_string (s ^ " : " )
; print_a_list_of_ints l
; print_newline ()
)
lists
; print_newline ()
)
;;
let () = main
;;