-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_function.sml
More file actions
36 lines (28 loc) · 800 Bytes
/
list_function.sml
File metadata and controls
36 lines (28 loc) · 800 Bytes
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
fun sum_list (xs : int list) =
if null xs
then 0
else hd xs + sum_list(tl xs)
fun countdown (x : int) =
if x=0
then []
else x :: countdown(x-1)
fun append (xs : int list, ys : int list) =
if null xs
then ys
else (hd xs) :: (append((tl xs), ys))
(* (int list) * (int list) -> int list *)
(* function over pairs of lists *)
fun sum_pair_list (xs : (int * int) list) =
if null xs
then 0
else #1 (hd xs) + #2 (hd xs) + sum_pair_list(tl xs)
fun firsts (xs : (int * int) list) =
if null xs
then []
else (#1 (hd xs)) :: firsts(tl xs)
fun seconds (xs : (int * int) list) =
if null xs
then []
else (#2 (hd xs)) :: seconds(tl xs)
fun sum_pair_list2 (xs : (int * int) list) =
sum_list(firsts xs) + sum_list (seconds xs)