-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.sml
executable file
·81 lines (65 loc) · 1.45 KB
/
test.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
71
72
73
74
75
76
77
78
79
80
81
val x = 3;
fun pow(x, y) =
if y=0
then 1
else x * pow(x, y - 1);
fun cube (x) =
pow(x, 3);
fun swap(pr: int*bool) =
(#2 pr, #1 pr);
fun sum_two_pairs(pr1: int*int, pr2: int*int) =
(#1 pr1 + #1 pr2, #2 pr1 + #2 pr2);
fun summation(pr1: int*int, pr2: int*int) =
(#1 pr1) + (#2 pr1) + (#1 pr2) + (#2 pr2);
fun div_mod(x: int, y: int) =
(x div y, x mod y);
fun sort_pair(pr: int*int) =
if (#1 pr) < (#2 pr)
then pr
else ((#2 pr), (#1 pr));
fun sum_list (xs: int list) =
if null xs
then 0
else hd(xs) + sum_list(tl xs);
fun count_down (x: int) =
if x = 0
then []
else x::count_down(x - 1);
fun append(xs, 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 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);
val result = seconds([(1, 2), (3, 4), (5, 6)]);
fun count_up (x: int) =
let fun count(from: int) =
if from > x
then []
else from::count(from + 1)
in
count 1
end
val test_count = count_up(10);
fun good_max(xs: int list) =
if null xs
then 0
else if null (tl xs)
then hd xs
else let val temp = good_max(tl xs)
in
if hd xs > temp
then hd xs
else temp
end
val test_max = good_max([1,2,3,4,5,100,5,33,7]);