Programming Languages, Part A Week2 作业(含 challenge questions)
程序员文章站
2022-03-09 15:43:08
...
Programming Languages, Part A
Week2 作业(含 challenge questions)
(* 1 *)
fun is_older (d1:int * int * int, d2:int * int * int) =
if (#1 d1) <> (#1 d2)
then (#1 d1) < (#1 d2)
else
if (#2 d1) <> (#2 d2)
then (#2 d1) < (#2 d2)
else (#3 d1) < (#3 d2)
(* 2 *)
fun number_in_month (d: (int * int * int) list, m: int) =
if null d
then 0
else if #2 (hd d) = m
then 1 + number_in_month(tl d,m)
else number_in_month(tl d,m)
(* 3 *)
fun number_in_months (d:(int * int * int) list, m: int list) =
if null m
then 0
else number_in_month(d,hd m) + number_in_months(d,tl m)
(* 4 *)
fun dates_in_month (d: (int * int * int) list, m: int)=
if null d
then []
else
if (#2 (hd d)) = m
then (hd d) :: dates_in_month(tl d,m)
else dates_in_month(tl d,m)
(* 5 *)
fun dates_in_months (d: (int * int * int) list, m: int list)=
if null m
then []
else dates_in_month(d, hd m) @ dates_in_months(d, tl m)
(* 6 *)
fun get_nth (s: string list, n: int)=
if n=1
then hd s
else get_nth(tl s,n-1)
(* 7 *)
fun date_to_string (d:int * int * int)=
let
val months = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
val month = get_nth(months,#2 d);
in
month ^ " " ^ Int.toString(#3 d) ^ ", " ^ Int.toString(#1 d)
end
(* 8 *)
fun number_before_reaching_sum (sum: int, posList: int list)=
if sum > hd posList
then 1 + number_before_reaching_sum(sum - hd posList, tl posList)
else 0
(* 9 *)
fun what_month (day: int)=
let
val day_length = [31,28,31,30,31,30,31,31,30,31,30,31]
in
number_before_reaching_sum(day, day_length) + 1
end
(* 10 *)
fun month_range (day1: int, day2: int)=
if day1 > day2
then []
else
what_month(day1) :: month_range(day1 + 1, day2)
(* 11 *)
fun oldest (dates: (int * int * int) list)=
if null dates
then NONE
else
let
val tl_ans = oldest(tl dates)
in
if isSome tl_ans andalso is_older(valOf tl_ans, hd dates)
then tl_ans
else SOME (hd dates)
end
(* 12 *)
fun remove_duplicates (l : int list)=
let
fun remove_element(target: int, subList: int list)=
if null subList
then []
else if hd subList = target
then remove_element(target,tl subList)
else hd subList :: remove_element(target, tl subList)
in
if null l
then []
else hd l :: remove_duplicates(remove_element(hd l,l))
end
fun number_in_months_challenge(dates: (int * int * int) list, months : int list)=
number_in_months(dates, remove_duplicates(months))
fun dates_in_months_challenge(dates: (int * int * int) list, months : int list)=
dates_in_months(dates,remove_duplicates(months))
(* 13 *)
fun reasonable_date(date: int * int * int)=
let
fun is_leap_year (year : int)=
year mod 4 = 0 andalso year mod 100 <> 0 orelse year mod 400 = 0
val leap_year = [31,29,31,30,31,30,31,31,30,31,30,31]
val normal_year = [31,28,31,30,31,30,31,31,30,31,30,31]
fun get_nth(num: int list, n: int)=
if n=1
then hd num
else get_nth(tl num,n-1)
in
if #1 date <= 0 orelse #2 date < 1 orelse #2 date > 12 orelse #3 date < 1 orelse #3 date > 31
then false
else if is_leap_year(#1 date)
then #3 date <= get_nth(leap_year,#2 date)
else #3 date <= get_nth(normal_year,#2 date)
end
注:
1.
op ::
val it = fn: 'a * 'a list -> 'a list
op @
val it = fn: 'a list * 'a list -> 'a list
In other words, :: cons’s an element of type 'a to a list of 'a’s, while @ appends two lists containing elements of type 'a.
::是元素加到list
@ 是list合并
上一篇: 为什么要使用jquery
下一篇: 开发者如何构建个人影响力