forked from roca/SMLProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment_01.txt
116 lines (90 loc) · 3.44 KB
/
assignment_01.txt
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
val months = [Date.Jan,Date.Feb,Date.Mar,Date.Apr,Date.May,Date.Jun,Date.Jul,Date.Aug,Date.Sep,Date.Oct,Date.Nov,Date.Dec];
fun make_date(month:int,day:int,year:int) =
let
val new_date = {day=day,hour=19,minute=27,month=List.nth(months,month-1),offset=NONE,second=23,year=year};
in
Date.date new_date
end
(*
Using the library $SMLNJ-BASIS/basis.cm that comes with the SMLNJ .
This library already has Date and Time functions build in.
*)
(* 1 val is_older = fn : (int * int * int) * (int * int * int) -> bool *)
fun is_older(date1 : (int * int * int), date2: (int * int * int)) =
let
val d1 = make_date(#1 date1,#2 date1, #3 date1);
val d2 = make_date(#1 date2,#2 date2, #3 date2);
in
if Date.compare(d1,d2) = LESS
then true
else false
end
(* 2 val number_in_month = fn : (int * int * int) list * int -> int *)
fun number_in_month(dates : (int * int * int) list, nmonth : int) =
let fun month_check( date : (int * int * int)) =
if Date.month((make_date date)) = List.nth(months,nmonth-1)
then 1
else 0
in
if null dates
then 0
else month_check(hd dates) + number_in_month( tl dates, nmonth)
end
(* 3 val number_in_months = fn : (int * int * int) list * int list -> int *)
fun number_in_months(dates : (int * int * int) list, nmonths : int list) =
if null dates orelse null nmonths
then 0
else number_in_month(dates , hd nmonths) + number_in_months(dates , tl nmonths )
(* 4 val dates_in_month = fn : (int * int * int) list * int -> (int * int * int) list *)
fun dates_in_month(dates : (int * int * int) list, nmonth : int) =
let fun month_check( date : (int * int * int)) =
if Date.month((make_date date)) = List.nth(months,nmonth-1)
then [date]
else []
in
if null dates
then []
else month_check(hd dates) @ dates_in_month( tl dates, nmonth)
end
(* 5 val dates_in_months = fn : (int * int * int) list * int list -> (int * int * int) list *)
fun dates_in_months(dates : (int * int * int) list, nmonths : int list) =
if null dates orelse null nmonths
then []
else dates_in_month(dates , hd nmonths) @ dates_in_months(dates , tl nmonths )
(* 6 val get_nth = fn : string list * int -> string *)
fun get_nth(strings : string list, n : int) =
let fun count_up_to(shorter_strings : string list,i : int) =
if i = n
then hd shorter_strings
else count_up_to(tl shorter_strings,i + 1)
in
if null strings
then hd strings
else count_up_to(strings,1)
end
fun get_nth2(strings : string list, n : int) =
List.nth(strings, n-1 )
(* 7 val = date_to_string = fn : int * int * int -> string *)
fun date_to_string(date : (int * int * int)) =
let
val nmonth = #1 date;
val day = #2 date;
val year = #3 date;
val month=List.nth(months,nmonth -1 );
val a_date = Date.date {day=day,hour=19,minute=27,month=month,offset=NONE,second=23,year=year};
in
Date.fmt "%B %d, %Y" a_date
end
(* 8 val number_before_reaching_sum = fn : int * int list -> int *)
fun number_before_reaching_sum(n : int , ns : int list) =
let
fun sum_up_to(n : int ,ns : int list) =
if (n - hd(ns)) <= 0
then 0
else hd(ns) + sum_up_to( n - hd(ns), tl(ns))
in
sum_up_to(n,ns)
end
(* 9 val what_month = fn : int -> int *)
(* 10 val month_range = fn : int * int -> int list *)
(* 11 val oldest = fn : (int * int * int) list -> (int * int * int) option *)