-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.sml
45 lines (35 loc) · 988 Bytes
/
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
fun pow(x : int, y : int) =
if y = 0
then 1
else x * pow(x,y-1)
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) = (* part of the course logo :) *)
if null xs
then ys
else hd(xs) :: append(tl(xs), ys)
(* More functions over lists, here lists of pairs of ints *)
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))
fun product_list (xs : int list) =
if null xs
then 1
else hd(xs) * product_list(tl(xs))