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

本文介绍了一系列日期相关的函数,包括比较日期、统计特定月份的日期数量、转换日期格式等,为日期数据分析提供了实用工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实作业不是很难,而且只要你提交后分数到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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值