-
Notifications
You must be signed in to change notification settings - Fork 0
/
Programming Languages part A
141 lines (128 loc) · 2.8 KB
/
Programming Languages part A
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(* 1 *)
fun is_older(xs:(int*int*int), ys: (int*int*int))=
let val first_year = #1 xs
val first_month = #2 xs
val first_day = #3 xs
val second_year = #1 ys
val second_month = #2 ys
val second_day = #3 ys
in
if first_year < second_year
then true
else if first_year = second_year andalso first_month <second_month
then true
else if first_year = second_year andalso first_month=second_month andalso first_day<second_day
then true
else false
end
(* 2 *)
fun number_in_month(xs:(int*int*int) list , y:int)=
if null xs
then 0
else
let
val date = hd xs
val month = #2 date
in
if month=y
then 1+number_in_month(tl xs,y)
else number_in_month(tl xs,y)
end
(* 3 *)
fun number_in_months(xs:(int*int*int) list, ys: int list)=
if null ys
then 0
else let
val y = hd ys
in
number_in_month(xs,y)+number_in_months(xs,tl ys)
end
(* 4 *)
fun dates_in_month(xs: (int*int*int) list, y: int)=
if null xs
then []
else if #2 (hd xs) = y
then hd xs :: dates_in_month(tl xs,y)
else dates_in_month(tl xs,y)
(* 5 *)
fun dates_in_months(xs:(int*int*int) list, ys:int list)=
if null ys
then []
else let
val y = hd ys
val a = dates_in_month(xs,y)
in
if null a
then dates_in_months(xs,tl ys)
else
a::dates_in_months(xs,tl ys)
end
(* 6 *)
fun get_nth(xs: string list, y:int)=
if y<=0 orelse null xs
then ""
else if y = 1
then hd xs
else
let val z = y-1
in
get_nth(tl xs,z)
end
(* 7 *)
fun date_to_string(x:(int*int*int))=
let
val year = Int.toString(#1 x)
val months = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"]
val day = Int.toString(#3 x)
val month = #2 x
val stringMonth = get_nth(months, month)
in
stringMonth^" "^day ^", " ^year
end
(* 8 *)
fun number_before_reaching_sum(sum : int,ws:int list) =
let fun add(x:int,ys:int list, z:int)=
if x+hd ys<sum
then add(x+hd ys, tl ys,z+1)
else z
in
add(0,ws,0)
end
(* 9 *)
fun what_month(x:int)=
let val month = [31,28,31,30,31,30,31,31,30,31,30,31]
in
number_before_reaching_sum(x,month)+1
end
(* 10 *)
fun month_range(x:int, y:int)=
if x>y
then []
else
let fun createList(xs:int list)=
if hd xs = y
then xs
else createList(hd xs +1::xs)
val range = createList([x])
fun series(ys:int list,zs:int list)=
if null ys
then zs
else
series(tl ys,what_month(hd ys)::zs)
in
series(range,[])
end
(* 11 *)
fun oldest(xs: (int*int*int) list)=
if null xs
then NONE
else
let
val tl_oldest = oldest(tl xs)
in
if isSome tl_oldest andalso not (is_older(hd xs, valOf tl_oldest))
then
tl_oldest
else
SOME(hd xs)
end