-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path08 Notes: functions over lists.sml
50 lines (34 loc) · 1.46 KB
/
08 Notes: functions over lists.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
(* functions on list *)
(* fxn to add up all the numbers in the list *)
fun sum_list ( xs : int list) =
if null xs
then 0
else hd xs + sum_list( tl xs)
fun mul_list ( xs : int list ) =
if null xs
then 1
else
hd xs * mul_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)
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 sum_pair_resolve ( xs : (int * int) list ) =
if null xs
then []
else #1 (hd xs) + #2 (hd xs) :: sum_pair_resolve (tl xs)
fun first ( xs : (int * int) list ) =
if null xs
then []
else #1 (hd xs) :: first ( tl xs)
(* things to remember:
- functions over list are usually recursive, that's the only way to 'get' to all the elements.
*)