1 / 23

Homework1

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]

mercia
Download Presentation

Homework1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Homework1 5100379002 冯云平

  2. Whatwehavelearnt • Variablebinding:val x = e; • Conditionals:ife1thene2elsee3 • Functionbinding:funx0 (x1 : t1, ..., xn : tn) = e • Tuples: • valtuple=(1,bool,“a”) • #1tuple

  3. Whatwehavelearnt • Lists: • valxs=[e1,e2,…,en] • e1::xs • nullxs • hdxs • tlxs

  4. Whatwehavelearnt • Let expressions: • let b1 b2 ... bn in e end • Options: • NONE / SOME e • isSome • valOf • Others: • andalso/orelse • e1=e2/e1<>e2/~1

  5. 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))

  6. 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)

  7. 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)

  8. 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)

  9. 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)

  10. 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)

  11. 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

  12. 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

  13. 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

  14. 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)

  15. 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

  16. 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))

  17. 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

  18. fundates_in_months_challenge (dates : ( int * int* int) list, months : int list) = if null months then [] elsedates_in_months (dates, remove_duplicates(months))

  19. Challenge Problem2

  20. 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) ……

  21. …… 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 ……

  22. …… in valid_yearandalsovalid_monthandalsovalid_day end

  23. Q & A

More Related