230 likes | 395 Views
Homework1. 5100379002 冯云平. What we have learnt. Variable binding : val x = e; Conditionals : if e1 then e2 else e3 F unction binding : fun x0 (x1 : t1, ..., xn : tn) = e Tuples : val tuple = ( 1, bool , “a” ) # 1 tuple. What we have learnt. Lists : val x s = [e1, e2, … , en]
E N D
Homework1 5100379002 冯云平
Whatwehavelearnt • Variablebinding:val x = e; • Conditionals:ife1thene2elsee3 • Functionbinding:funx0 (x1 : t1, ..., xn : tn) = e • Tuples: • valtuple=(1,bool,“a”) • #1tuple
Whatwehavelearnt • Lists: • valxs=[e1,e2,…,en] • e1::xs • nullxs • hdxs • tlxs
Whatwehavelearnt • Let expressions: • let b1 b2 ... bn in e end • Options: • NONE / SOME e • isSome • valOf • Others: • andalso/orelse • e1=e2/e1<>e2/~1
Problem1 • fun is_older (pr1 : int * int * int, pr2: int * int * int) :bool funis_older (pr1 : int * int * int, pr2: int * int * int) = (#1 pr1 <#1 pr2) orelse ( (#1 pr1 = #1 pr2) andalso (#2 pr1 <#2 pr2) ) orelse ( (#1 pr1 = #1 pr2) andalso (#2 pr1 = #2 pr2) andalso (#3 pr1 <#3 pr2))
Problem2 • funnumber_in_month(dates: (int * int * int) list ,month:int ):int funnumber_in_month(dates: (int*int*int) list, month: int) = ifnull dates then0 else if#2 (hd dates) = month then1 + number_in_month(tl dates, month) elsenumber_in_month(tl dates, month)
Problem3 • fun number_in_months (dates : ( int * int * int ) list, months : int list) :int funnumber_in_months(dates: (int*int*int) list, months: int list) = if null months then 0 elsenumber_in_month(dates, hd months) + number_in_months(dates, tl months)
Problem4 • fun dates_in_month(dates: (int*int*int) list, month: int):(int*int*int) list fundates_in_month (dates : ( int * int * int ) list, month : int) = ifnull dates then [] else if#2 (hd dates) = month then(hd dates) :: dates_in_month ( (tl dates), month) elsedates_in_month ( (tl dates), month)
Problem5 • fun dates_in_months (dates : ( int * int * int ) list, months : int list):( int * int * int ) list fundates_in_months (dates : ( int * int * int ) list, months : int list) = if null months then [] elsedates_in_month (dates, hd months) @ dates_in_months (dates, tl months)
Problem6 • fun get_nth(strings: string list, n: int):string funget_nth(strings: string list, n: int) = ifn = 1 thenhd strings elseget_nth(tl strings, n-1)
Problem7 • fun date_to_string ( date : int * int * int):string fundate_to_string ( date : int * int * int) = let valmonths_name = ["January", "February", "March", "April", "May", "June”, "July", "August", "September", "October", "November", "December"] in get_nth (months_name, #2 date) ^ “ ” ^ Int.toString (#3 date) ^ ", " ^ Int.toString (#1 date) end
Problem8 • fun number_before_reaching_sum(sum: int, numbers: int list):int funnumber_before_reaching_sum(sum: int, numbers: int list) = let funcount(numbers: int list, acc: int, n: int) = ifhd numbers + acc >= sum then n else count(tl numbers, acc + hd numbers, n + 1) in count(numbers, 0, 0) end
Promblem9 • fun what_month (day_of_year : int):int funwhat_month(day_of_year: int) = let valdays_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] in number_before_reaching_sum(day,days_in_month) + 1 end
Problem10 • fun month_range(day1: int, day2: int):intlist funmonth_range(day1: int, day2: int) = ifday1 > day2 then[] elsewhat_month(day1) :: month_range(day1 + 1, day2)
Problem11 • fun oldest(dates: (int*int*int) list):(int*int*int)option fun oldest(dates: (int*int*int) list) = ifnull dates thenNONE else let valtl_ans = oldest(tl dates) in ifisSometl_ansandalsois_older(valOftl_ans, hd dates) thentl_ans elseSOME (hd dates) end
Challenge Problem 1 • fun number_in_months_challenge (dates : ( int * int * int ) list, months : int list) :int funnumber_in_months_challenge (dates : ( int * int * int ) list, months : int list) = if null months then 0 elsenumber_in_months (dates, remove_duplicates(months))
funremove_duplicates (months : int list) = let fun exists (n : int, ls : int list) = if null ls thenfalse else(n = (hdls) orelse exists(n, tlls)) in if null months then [] else ifexists(hd months, tl months) thenremove_duplicates (tl months) else(hd months) :: remove_duplicates(tl months) end
fundates_in_months_challenge (dates : ( int * int* int) list, months : int list) = if null months then [] elsedates_in_months (dates, remove_duplicates(months))
funreasonable_date (date : int * int * int) = let valyear=#1date valmonth=#2date valday=#3date valvalid_year = year > 0 valvalid_month = (month >= 1) andalso(month<= 12) valis_leap_year = (year mod 400 = 0) orelse ((yearmod 4 = 0) andalso(yearmod 100) <> 0) ……
…… valval_for_Feb = ifis_leap_yearthen 29 else 28 val days = [31, val_for_Feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] funget_nth_int (ls : intlist, n : int) = if n = 1 thenhdls elseget_nth_int (tlls, (n-1)) valvalid_day = valid_monthandalso ((#3 date) <= get_nth_int(days, #2 date)) andalso((#3 date) >= 1); in ……
…… in valid_yearandalsovalid_monthandalsovalid_day end