-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_functions.sml
71 lines (60 loc) · 1.47 KB
/
list_functions.sml
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
(* collection of functions that work with lists *)
(* sum the elemenets of a list *)
fun sum_list (l : int list) =
if null l
then 0
else hd(l) + sum_list(tl l)
(* multiply the elements of a list *)
fun product_list (l : int list) =
if null l
then 1
else hd(l) * product_list(tl l)
(* list of count from, to integers *)
fun count (from : int, to : int) =
if from = to
then from :: []
else if from > to
then from :: count(from-1, to)
else from :: count(from+1, to)
(* list of a count down *)
fun countdown (x : int) =
if x = 0
then []
else x :: countdown(x-1)
(* append list within another list *)
fun append (l1 : int list, l2 : int list) =
if null l1
then l2
else hd(l1) :: append(tl(l1), l2)
(* list of first elements of list of pairs *)
fun firsts (l : (int * int) list) =
if null l
then []
else (#1 (hd(l))) :: (firsts(tl l))
(* list of second elements of list of pairs *)
fun seconds (l : (int * int) list) =
if null l
then []
else (#2 (hd(l))) :: (seconds(tl l))
(* sum the pair elements of integers of a list *)
fun sum_pair_list (l : (int * int) list) =
sum_list(firsts(l)) + sum_list(seconds(l))
(* list of count up till an integer *)
fun countup_till (x : int) =
let fun count (from : int) =
if from = x
then x :: []
else from :: count(from + 1)
in
count 1
end
(* max value of a list *)
fun max (l : int list) =
if null l
then NONE
else let val max_tl = max(tl l)
in
if isSome max_tl andalso valOf max_tl > hd l
then max_tl
else SOME(hd l)
end