programming language part a 第二周作业

it2024-12-06  18

 

(*Homework1*) (*2018-3-11*) (*zhemuwoa*) (*1*) fun is_older(da1:int*int*int,da2:int*int*int) = let val y1 = #1 da1 val y2 = #1 da2 val m1 = #2 da1 val m2 = #2 da2 val d1 = #3 da1 val d2 = #3 da2 in y1 < y2 orelse (y1 = y2 andalso m1 < m2) orelse (y1 = y2 andalso m1 = m2 andalso d1 < d2) end (*2*) fun number_in_month(dl:(int*int*int) list,mo:int) = if null dl then 0 else if #2 (hd dl) = mo then 1 + number_in_month(tl dl,mo) else number_in_month(tl dl,mo) (*3*) fun number_in_months(dl:(int * int * int) list , ml : int list) = if null ml then 0 else number_in_month(dl,hd ml)+number_in_months(dl,tl ml) (*4*) fun dates_in_month(dl:(int * int * int) list , mo : int) = if null dl then [] else if #2 (hd dl) = mo then hd dl :: dates_in_month(tl dl,mo) else dates_in_month(tl dl,mo) (*5*) fun dates_in_months(dl:(int * int * int) list,ml : int list) = if null ml then [] else dates_in_month(dl,hd ml) @ dates_in_months(dl,tl ml) (*6*) fun get_nth(sl:string list,n:int) = if n = 1 then hd sl else get_nth(tl sl,n-1) (*7*) fun date_to_string(da:(int*int*int)) = let val mtos=["January","February","March","April","May","June", "July","August","September","October","November","December"] val year=(#1 da) val month=(#2 da) val day=(#3 da) in get_nth(mtos,month)^" "^Int.toString(day)^", "^Int.toString(year) end (*9*) fun number_before_reaching_sum(sum:int,il:int list) = if sum <= 0 then ~1 else number_before_reaching_sum(sum-(hd il),tl il)+1 (*10*) fun what_month(doy:int) = let val md=[31,28,31,30,31,30,31,31,30,31,30,31] in number_before_reaching_sum(doy,md)+1 end fun month_range(day1:int,day2:int) = if day1 > day2 then [] else what_month(day1)::month_range(day1+1,day2) (*11*) fun oldest(dl:(int*int*int) list) = if null dl then NONE else let fun oldest_h(dl_x:(int*int*int) list) = if null (tl dl_x) then hd dl_x else let val ans=oldest_h(tl dl_x) in if is_older(hd dl_x,ans) then hd dl_x else ans end in SOME (oldest_h dl) end (*12*) fun rem_dup(xs:int list) = if null xs then [] else let fun is_in(e:int,xl:int list) = not (null xl) andalso (e = hd xl orelse is_in(e,tl xl)) val tl_ans=rem_dup(tl xs) in if is_in(hd xs,tl_ans) then tl_ans else hd xs :: tl_ans end fun number_in_months_challenge(da:(int*int*int) list,mo:int list) = number_in_months(da,rem_dup mo) fun dates_in_months_challenge(da:(int*int*int) list,mo:int list) = dates_in_months(da,rem_dup mo) (*13*) fun reasonable_date(date : int*int*int) = let val year = #1 date val month = #2 date val day = #3 date val isleap = year mod 400 = 0 orelse (year mod 4 = 0 andalso year mod 100 <> 0) val feb= if isleap then 29 else 28 val mon_l=[31,feb,31,30,31,30,31,31,30,31,30,31] fun get_nth(ml:int list,n:int) = if n = 1 then hd ml else get_nth(tl ml , n - 1) in year > 0 andalso month >= 1 andalso month <= 12 andalso day >= 1 andalso day <= get_nth(mon_l,month) end

 

如何判断闰年在一般情况下只要判断

是否是400 的倍数或者是4的倍数不是100的倍数

 

快捷键ctrl+c+ctrl+s 打开repl

ctrl+d关闭repl

meta+p回溯历史命令

转载于:https://www.cnblogs.com/tclan126/p/8544377.html

最新回复(0)