华盛顿大学程序设计语言第二周作业一答案

其实作业不是很难,而且只要你提交后分数到80以上就有标准答案提供。

fun is_older(d1:int*int*int,d2:int*int*int)=
    if (#1 d1)<(#1 d2)
    then true
    else if (#1 d1)=(#1 d2) andalso (#2 d1)<(#2 d2)
    then true
    else if (#1 d1)=(#1 d2) andalso (#2 d1)=(#2 d2) andalso (#3 d1)<(#3 d2)
    then true
    else false
fun number_in_month(lst:(int*int*int) list,mon: int) =
    if null lst
    then 0
    else if #2 (hd lst)=mon
    then (1+number_in_month(tl lst,mon))
    else number_in_month(tl lst,mon)
fun number_in_months(lst:(int*int*int) list,mon:int list)=
    if null mon
    then 0
    else number_in_month(lst,hd mon)+number_in_months(lst,tl mon)
fun dates_in_month(lst:(int*int*int) list,mon:int)=
    if null lst
    then []
    else if #2 (hd lst)=mon
    then hd lst::dates_in_month(tl lst,mon)
    else dates_in_month(tl lst,mon)
fun dates_in_months(lst:(int*int*int) list,mon:int list)=
    if null mon
    then []
    else dates_in_month(lst,hd mon)@dates_in_months(lst,tl mon)

fun get_nth(strs:string list,n:int)=
    if n=1
    then hd strs
    else get_nth(tl strs,n-1)
fun date_to_string(date:(int*int*int))=
    let val months:string list=["January"," February","March", "April","May", "June", "July", "August", "September", "October", "November", "December"]
    in get_nth(months,#2 date)^" "^Int.toString(#3 date)^", "^Int.toString(#1 date)
    end
fun number_before_reaching_sum(sum:int,data:int list)=
    if sum<=hd data
    then 0
    else 1+number_before_reaching_sum(sum-hd data,tl data)
fun what_month(day:int)=
    let val months=[31,28,31,30,31,30,31,31,30,31,30,31]
    in number_before_reaching_sum(day,months)+1
    end
fun month_range(day1:int,day2:int)=
    if day1>day2
    then []
    else what_month(day1)::month_range(day1+1,day2)
fun oldest(dates:(int*int*int) list)=
    if null dates
    then NONE
    else let val tllast=(oldest(tl dates))
     in if (not (isSome(tllast))) orelse (is_older(hd dates,valOf(tllast)))
        then SOME(hd dates)
        else tllast
     end
fun same(data:int list,a:int)=
    (not (null data)) andalso ((hd data=a) orelse same(tl data,a))
fun remove_the_same(data:int list)=
    if null data orelse null (tl data)
    then data
    else if same(tl data,hd data)
    then remove_the_same(tl data)
    else hd data::remove_the_same(tl data)
fun number_in_months_challenge(dates:(int*int*int) list,mon:int list)=
    number_in_months(dates,remove_the_same(mon))
fun dates_in_months_challenge(dates:(int*int*int) list,mon:int list)=
    dates_in_months(dates,remove_the_same(mon))
fun get_nth_for_int(data:int list,n:int)=
    if n=1
    then hd data
    else get_nth_for_int(tl data,n-1)
fun reasonable_date(date:(int*int*int))=
    if (#1 date<=0) orelse (#2 date<=0) orelse (#2 date>12) orelse(#3 date<=0)
    then false
    else if ((#1 date)mod 400=0) orelse ((#1 date)mod 4=0 andalso (#1 date)mod 100<>0 )
    then let val months=[31,29,31,30,31,30,31,31,30,31,30,31]
     in not(#3 date>get_nth_for_int(months,#2 date))
     end
    else let val months=[31,28,31,30,31,30,31,31,30,31,30,31]
     in (#3 date>get_nth_for_int(months,#2 date))
     end

猜你喜欢

转载自blog.csdn.net/qq_38184698/article/details/82500988
今日推荐