Download presentation

Presentation is loading. Please wait.

Published byOsbaldo Bransford Modified over 4 years ago

1
Homework1 5100379002

2
What we have learnt Variable binding: val x = e; Conditionals: if e1 then e2 else e3 Function binding:fun x0 (x1 : t1,..., xn : tn) = e Tuples: – val tuple = (1, bool, a) – #1 tuple

3
What we have learnt Lists: – val xs = [e1, e2, …, en] – e1 :: xs – null xs – hd xs – tl xs

4
What we have learnt 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 fun is_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 fun number_in_month (dates: (int * int * int) list,month: int ): int fun number_in_month(dates: (int*int*int) list, month: int) = if null dates then 0 else if #2 (hd dates) = month then 1 + number_in_month(tl dates, month) else number_in_month(tl dates, month)

7
Problem3 fun number_in_months (dates : ( int * int * int ) list, months : int list) : int fun number_in_months(dates: (int*int*int) list, months: int list) = if null months then 0 else number_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 fun dates_in_month (dates : ( int * int * int ) list, month : int) = if null dates then [] else if #2 (hd dates) = month then (hd dates) :: dates_in_month ( (tl dates), month) else dates_in_month ( (tl dates), month)

9
Problem5 fun dates_in_months (dates : ( int * int * int ) list, months : int list) : ( int * int * int ) list fun dates_in_months (dates : ( int * int * int ) list, months : int list) = if null months then [] else dates_in_month (dates, hd months) @ dates_in_months (dates, tl months)

10
Problem6 fun get_nth(strings: string list, n: int):string fun get_nth(strings: string list, n: int) = if n = 1 then hd strings else get_nth(tl strings, n-1)

11
Problem7 fun date_to_string ( date : int * int * int): string fun date_to_string ( date : int * int * int) = let val months_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 fun number_before_reaching_sum(sum: int, numbers: int list) = let fun count(numbers: int list, acc: int, n: int) = if hd 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 fun what_month(day_of_year: int) = let val days_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): int list fun month_range(day1: int, day2: int) = if day1 > day2 then [] else what_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) = if null dates then NONE else let val tl_ans = oldest(tl dates) in if isSome tl_ans andalso is_older(valOf tl_ans, hd dates) then tl_ans else SOME (hd dates) end

16
Challenge Problem 1 fun number_in_months_challenge (dates : ( int * int * int ) list, months : int list) : int fun number_in_months_challenge (dates : ( int * int * int ) list, months : int list) = if null months then 0 else number_in_months (dates, remove_duplicates(months))

17
fun remove_duplicates (months : int list) = let fun exists (n : int, ls : int list) = if null ls then false else (n = (hd ls) orelse exists(n, tl ls)) in if null months then [] else if exists(hd months, tl months) then remove_duplicates (tl months) else (hd months) :: remove_duplicates(tl months) end

18
fun dates_in_months_challenge (dates : ( int * int * int ) list, months : int list) = if null months then [] else dates_in_months (dates, remove_duplicates(months))

19
Challenge Problem2

20
fun reasonable_date (date : int * int * int) = let val year = #1 date val month = #2 date val day = #3 date val valid_year = year > 0 val valid_month = (month >= 1) andalso (month <= 12) val is_leap_year = (year mod 400 = 0) orelse ((year mod 4 = 0) andalso (year mod 100) <> 0) ……

21
val val_for_Feb = if is_leap_year then 29 else 28 val days = [31, val_for_Feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] fun get_nth_int (ls : int list, n : int) = if n = 1 then hd ls else get_nth_int (tl ls, (n-1)) val valid_day = valid_month andalso ((#3 date) <= get_nth_int(days, #2 date)) andalso ((#3 date) >= 1); in ……

22
in valid_year andalso valid_month andalso valid_day end

23
Q & A

Similar presentations

OK

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google