Difference between revisions of "Calendar Date Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=Hints and Gotchas=
 
=Hints and Gotchas=
[https://www.coursera.org/learn/programming-languages/supplement/MHfZa/hints-and-gotchas-for-homework-1 Hints and Gotchas] credit to Charilaos Skiadas
+
[https://www.coursera.org/learn/programming-languages/supplement/MHfZa/hints-and-gotchas-for-homework-1 Hints and Gotchas] credit to [https://vault.hanover.edu/~skiadas/ Charilaos Skiadas]
 +
 
 +
=Credit=
 +
{{GrossmanCredit}}
  
=Instructions=
 
 
[https://courses.cs.washington.edu/courses/cse341/13sp/hw1.pdf UW] or [https://www.coursera.org/learn/programming-languages/programming/ZTeNx/homework-1-instructions Coursera]
 
[https://courses.cs.washington.edu/courses/cse341/13sp/hw1.pdf UW] or [https://www.coursera.org/learn/programming-languages/programming/ZTeNx/homework-1-instructions Coursera]
  
Line 14: Line 16:
 
where, for example, 33 represents February 2. (We ignore leap years except in one challenge problem.)
 
where, for example, 33 represents February 2. (We ignore leap years except in one challenge problem.)
  
==is_older==
+
{{SMLToImplement|uw1|is_older<br/>number_in_month<br/>number_in_months<br/>dates_in_month<br/>dates_in_months<br/>get_nth<br/>date_to_string<br/>number_before_reaching_sum<br/>what_month<br/>month_range<br/>oldest<br/>number_in_months_challenge<br/>dates_in_months_challenge<br/>reasonable_date|uw1}}
 +
 
 +
==1) is_older==
 
Write a function <code>is_older</code> that takes two dates and evaluates to true or false. It evaluates to true if
 
Write a function <code>is_older</code> that takes two dates and evaluates to true or false. It evaluates to true if
 
the first argument is a date that comes before the second argument. (If the two dates are the same,
 
the first argument is a date that comes before the second argument. (If the two dates are the same,
 
the result is false.)
 
the result is false.)
==number_in_month==
+
==2) number_in_month==
 
Write a function <code>number_in_month</code> that takes a list of dates and a month (i.e., an <code>int</code>) and returns
 
Write a function <code>number_in_month</code> that takes a list of dates and a month (i.e., an <code>int</code>) and returns
 
how many dates in the list are in the given month.
 
how many dates in the list are in the given month.
==number_in_months==
+
==3) number_in_months==
 
Write a function <code>number_in_months</code> that takes a list of dates and a list of months (i.e., an <code>int list</code>)
 
Write a function <code>number_in_months</code> that takes a list of dates and a list of months (i.e., an <code>int list</code>)
 
and returns the number of dates in the list of dates that are in any of the months in the list of months.
 
and returns the number of dates in the list of dates that are in any of the months in the list of months.
 
''Assume the list of months has no number repeated.'' Hint: Use your answer to the previous problem.
 
''Assume the list of months has no number repeated.'' Hint: Use your answer to the previous problem.
==dates_in_month==
+
==4) dates_in_month==
 
Write a function <code>dates_in_month</code> that takes a list of dates and a month (i.e., an <code>int</code>) and returns a
 
Write a function <code>dates_in_month</code> that takes a list of dates and a month (i.e., an <code>int</code>) and returns a
 
list holding the dates from the argument list of dates that are in the month. The returned list should
 
list holding the dates from the argument list of dates that are in the month. The returned list should
 
contain dates in the order they were originally given.
 
contain dates in the order they were originally given.
==dates_in_months==
+
==5) dates_in_months==
 
Write a function <code>dates_in_months</code> that takes a list of dates and a list of months (i.e., an <code>int list</code>)
 
Write a function <code>dates_in_months</code> that takes a list of dates and a list of months (i.e., an <code>int list</code>)
 
and returns a list holding the dates from the argument list of dates that are in any of the months in
 
and returns a list holding the dates from the argument list of dates that are in any of the months in
 
the list of months. ''Assume the list of months has no number repeated.'' Hint: Use your answer to the
 
the list of months. ''Assume the list of months has no number repeated.'' Hint: Use your answer to the
 
previous problem and SML’s list-append operator ([https://smlfamily.github.io/Basis/list.html#SIG:LIST.@:VAL @]).
 
previous problem and SML’s list-append operator ([https://smlfamily.github.io/Basis/list.html#SIG:LIST.@:VAL @]).
==get_nth==
+
==6) get_nth==
 
Write a function <code>get_nth</code> that takes a list of strings and an <code>int</code> n and returns the nth element of the
 
Write a function <code>get_nth</code> that takes a list of strings and an <code>int</code> n and returns the nth element of the
 
list where the head of the list is 1st. Do not worry about the case where the list has too few elements:
 
list where the head of the list is 1st. Do not worry about the case where the list has too few elements:
 
your function may apply <code>hd</code> or <code>tl</code> to the empty list in this case, which is okay.
 
your function may apply <code>hd</code> or <code>tl</code> to the empty list in this case, which is okay.
==date_to_string==
+
==7) date_to_string==
 
Write a function <code>date_to_string</code> that takes a date and returns a string of the form January 20, 2013
 
Write a function <code>date_to_string</code> that takes a date and returns a string of the form January 20, 2013
 
(for example). Use the operator <code>^</code> for concatenating strings and the library function <code>Int.toString</code>
 
(for example). Use the operator <code>^</code> for concatenating strings and the library function <code>Int.toString</code>
Line 45: Line 49:
 
comma following the day and use capitalized English month names: January, February, March, April,
 
comma following the day and use capitalized English month names: January, February, March, April,
 
May, June, July, August, September, October, November, December.
 
May, June, July, August, September, October, November, December.
==number_before_reaching_sum==
+
==8) number_before_reaching_sum==
 
Write a function <code>number_before_reaching_sum</code> that takes an <code>int</code> called ''sum'', which you can assume
 
Write a function <code>number_before_reaching_sum</code> that takes an <code>int</code> called ''sum'', which you can assume
 
is positive, and an <code>int list</code>, which you can assume contains all positive numbers, and returns an <code>int</code>.
 
is positive, and an <code>int list</code>, which you can assume contains all positive numbers, and returns an <code>int</code>.
Line 51: Line 55:
 
''n + 1'' elements of the list add to ''sum'' or more. Assume the entire list sums to more than the passed in
 
''n + 1'' elements of the list add to ''sum'' or more. Assume the entire list sums to more than the passed in
 
value; it is okay for an exception to occur if this is not the case.
 
value; it is okay for an exception to occur if this is not the case.
==what_month==
+
==9) what_month==
 
Write a function <code>what_month</code> that takes a day of year (i.e., an int between 1 and 365) and returns
 
Write a function <code>what_month</code> that takes a day of year (i.e., an int between 1 and 365) and returns
 
what month that day is in (1 for January, 2 for February, etc.). Use a list holding 12 integers and your
 
what month that day is in (1 for January, 2 for February, etc.). Use a list holding 12 integers and your
 
answer to the previous problem.
 
answer to the previous problem.
==month_range==
+
==10) month_range==
 
Write a function <code>month_range</code> that takes two days of the year day1 and day2 and returns an <code>int list
 
Write a function <code>month_range</code> that takes two days of the year day1 and day2 and returns an <code>int list
 
[m1,m2,...,mn]</code> where m1 is the month of day1, m2 is the month of day1+1, ..., and mn is the month
 
[m1,m2,...,mn]</code> where m1 is the month of day1, m2 is the month of day1+1, ..., and mn is the month
 
of day day2. Note the result will have length day2 - day1 + 1 or length 0 if day1>day2.
 
of day day2. Note the result will have length day2 - day1 + 1 or length 0 if day1>day2.
==oldest==
+
==11) oldest==
 
Write a function <code>oldest</code> that takes a list of dates and evaluates to an <code>(int*int*int) option</code>. It
 
Write a function <code>oldest</code> that takes a list of dates and evaluates to an <code>(int*int*int) option</code>. It
 
evaluates to <code>NONE</code> if the list has no dates and <code>SOME d</code> if the date d is the oldest date in the list.
 
evaluates to <code>NONE</code> if the list has no dates and <code>SOME d</code> if the date d is the oldest date in the list.
==number_in_months_challenge==
+
==12) number_in_months_challenge, and dates_in_months_challenge==
Challenge Problem: Write functions number_in_months_challenge and dates_in_months_challenge
+
Challenge Problem: Write functions <code>number_in_months_challenge</code> and <code>dates_in_months_challenge</code>
 
that are like your solutions to problems 3 and 5 except having a month in the second argument multiple
 
that are like your solutions to problems 3 and 5 except having a month in the second argument multiple
times has no more effect than having it once. (Hint: Remove duplicates, then use previous work.)
+
times has no more effect than having it once. (Hint: remove duplicates, then use previous work.)
==reasonable_date==
+
 
 +
==13) reasonable_date==
 
Challenge Problem: Write a function <code>reasonable_date</code> that takes a date and determines if it
 
Challenge Problem: Write a function <code>reasonable_date</code> that takes a date and determines if it
 
describes a real date in the common era. A “real date” has a positive year (year 0 did not exist), a
 
describes a real date in the common era. A “real date” has a positive year (year 0 did not exist), a
Line 72: Line 77:
 
years. Leap years are years that are either divisible by 400 or divisible by 4 but not divisible by 100.
 
years. Leap years are years that are either divisible by 400 or divisible by 4 but not divisible by 100.
 
(Do not worry about days possibly lost in the conversion to the Gregorian calendar in the Late 1500s.)
 
(Do not worry about days possibly lost in the conversion to the Gregorian calendar in the Late 1500s.)
Note: Remember challenge problems are not required for a high grade and will be worth (only) a few points.
+
<!--Note: Remember challenge problems are not required for a high grade and will be worth (only) a few points.
Note: The sample solution contains roughly 75–80 lines of code, not including challenge problems.
+
 
 +
Note: The sample solution contains roughly 75–80 lines of code, not including challenge problems.-->
  
 
=Syntax Hints=
 
=Syntax Hints=
 
Small syntax errors can lead to strange error messages. Here are 3 examples for function definitions:
 
Small syntax errors can lead to strange error messages. Here are 3 examples for function definitions:
1. int * int * int list means int * int * (int list), not (int * int * int) list.
+
# <code>int * int * int list</code> means <code>int * int * (int list)</code>, not <code>(int * int * int) list</code>.
2. fun f x : t means the result type of f is t, whereas fun f (x:t) means the argument type of f is t.
+
# <code>fun f x : t</code> means the ''result type'' of f is t, whereas fun f (x:t) means the argument type of f is t. There is no need to write result types (and in later assignments, no need to write argument types).
There is no need to write result types (and in later assignments, no need to write argument types).
+
# <code>fun (x t)</code>, <code>fun (t x)</code>, or <code>fun (t : x)</code> are all wrong, but the error message suggests you are trying to do something much more advanced than you actually are (which is trying to write <code>fun (x : t)</code>).
3. fun (x t), fun (t x), or fun (t : x) are all wrong, but the error message suggests you are trying
+
 
to do something much more advanced than you actually are (which is trying to write fun (x : t)).
 
 
=Summary=
 
=Summary=
 
Evaluating a correct homework solution should generate these bindings:
 
Evaluating a correct homework solution should generate these bindings:
<nowiki>val is_older = fn : (int * int * int) * (int * int * int) -> bool
+
<syntaxhighlight lang="sml">
 +
val is_older = fn : (int * int * int) * (int * int * int) -> bool
 
val number_in_month = fn : (int * int * int) list * int -> int
 
val number_in_month = fn : (int * int * int) list * int -> int
 
val number_in_months = fn : (int * int * int) list * int list -> int
 
val number_in_months = fn : (int * int * int) list * int list -> int
Line 94: Line 100:
 
val what_month = fn : int -> int
 
val what_month = fn : int -> int
 
val month_range = fn : int * int -> int list
 
val month_range = fn : int * int -> int list
val oldest = fn : (int * int * int) list -> (int * int * int) option</nowiki>
+
val oldest = fn : (int * int * int) list -> (int * int * int) option
 +
val number_in_months_challenge = fn : (int * int * int) list * int list -> int
 +
val dates_in_months_challenge = fn : (int * int * int) list * int list -> (int * int * int) list
 +
val reasonable_date = fn : int * int * int -> bool
 +
</syntaxhighlight>
  
Of course, generating these bindings does not guarantee that your solutions are correct.  
+
Of course, generating these bindings does not guarantee that your solutions are correct.
  
 
=Test=
 
=Test=
Test your functions:
+
{{SMLUnitTesting|run_uw1_testing|uw1}}
 +
 
 +
Test your functions.  The unit tests you have been provided from UW are intentionally minimal.
  
 
=Pledge, Acknowledgments, Citations=
 
=Pledge, Acknowledgments, Citations=
{{Pledge|lab-1-calendar-date}}
+
{{Pledge|uw1-calendar-date}}

Latest revision as of 18:46, 10 July 2023

Hints and Gotchas

Hints and Gotchas credit to Charilaos Skiadas

Credit

All credit for this assignment goes to Prof. Grossman and his team at UW.

UW or Coursera

Code to Implement

You will write 11 SML functions (and tests for them) related to calendar dates. In all problems, a “date” is an SML value of type int*int*int, where the first part is the year, the second part is the month, and the third part is the day. A “reasonable” date has a positive year, a month between 1 and 12, and a day no greater than 31 (or less depending on the month). Your solutions need to work correctly only for reasonable dates, but do not check for reasonable dates (that is a challenge problem) and many of your functions will naturally work correctly for some/all non-reasonable dates. A “day of year” is a number from 1 to 365 where, for example, 33 represents February 2. (We ignore leap years except in one challenge problem.)

file: src/main/sml/uw1/uw1.sml Smlnj-logo.png
functions: is_older
number_in_month
number_in_months
dates_in_month
dates_in_months
get_nth
date_to_string
number_before_reaching_sum
what_month
month_range
oldest
number_in_months_challenge
dates_in_months_challenge
reasonable_date

1) is_older

Write a function is_older that takes two dates and evaluates to true or false. It evaluates to true if the first argument is a date that comes before the second argument. (If the two dates are the same, the result is false.)

2) number_in_month

Write a function number_in_month that takes a list of dates and a month (i.e., an int) and returns how many dates in the list are in the given month.

3) number_in_months

Write a function number_in_months that takes a list of dates and a list of months (i.e., an int list) and returns the number of dates in the list of dates that are in any of the months in the list of months. Assume the list of months has no number repeated. Hint: Use your answer to the previous problem.

4) dates_in_month

Write a function dates_in_month that takes a list of dates and a month (i.e., an int) and returns a list holding the dates from the argument list of dates that are in the month. The returned list should contain dates in the order they were originally given.

5) dates_in_months

Write a function dates_in_months that takes a list of dates and a list of months (i.e., an int list) and returns a list holding the dates from the argument list of dates that are in any of the months in the list of months. Assume the list of months has no number repeated. Hint: Use your answer to the previous problem and SML’s list-append operator (@).

6) get_nth

Write a function get_nth that takes a list of strings and an int n and returns the nth element of the list where the head of the list is 1st. Do not worry about the case where the list has too few elements: your function may apply hd or tl to the empty list in this case, which is okay.

7) date_to_string

Write a function date_to_string that takes a date and returns a string of the form January 20, 2013 (for example). Use the operator ^ for concatenating strings and the library function Int.toString for converting an int to a string. For producing the month part, do not use a bunch of conditionals. Instead, use a list holding 12 strings and your answer to the previous problem. For consistency, put a comma following the day and use capitalized English month names: January, February, March, April, May, June, July, August, September, October, November, December.

8) number_before_reaching_sum

Write a function number_before_reaching_sum that takes an int called sum, which you can assume is positive, and an int list, which you can assume contains all positive numbers, and returns an int. You should return an int n such that the first n elements of the list add to less than sum, but the first n + 1 elements of the list add to sum or more. Assume the entire list sums to more than the passed in value; it is okay for an exception to occur if this is not the case.

9) what_month

Write a function what_month that takes a day of year (i.e., an int between 1 and 365) and returns what month that day is in (1 for January, 2 for February, etc.). Use a list holding 12 integers and your answer to the previous problem.

10) month_range

Write a function month_range that takes two days of the year day1 and day2 and returns an int list [m1,m2,...,mn] where m1 is the month of day1, m2 is the month of day1+1, ..., and mn is the month of day day2. Note the result will have length day2 - day1 + 1 or length 0 if day1>day2.

11) oldest

Write a function oldest that takes a list of dates and evaluates to an (int*int*int) option. It evaluates to NONE if the list has no dates and SOME d if the date d is the oldest date in the list.

12) number_in_months_challenge, and dates_in_months_challenge

Challenge Problem: Write functions number_in_months_challenge and dates_in_months_challenge that are like your solutions to problems 3 and 5 except having a month in the second argument multiple times has no more effect than having it once. (Hint: remove duplicates, then use previous work.)

13) reasonable_date

Challenge Problem: Write a function reasonable_date that takes a date and determines if it describes a real date in the common era. A “real date” has a positive year (year 0 did not exist), a month between 1 and 12, and a day appropriate for the month. Solutions should properly handle leap years. Leap years are years that are either divisible by 400 or divisible by 4 but not divisible by 100. (Do not worry about days possibly lost in the conversion to the Gregorian calendar in the Late 1500s.)

Syntax Hints

Small syntax errors can lead to strange error messages. Here are 3 examples for function definitions:

  1. int * int * int list means int * int * (int list), not (int * int * int) list.
  2. fun f x : t means the result type of f is t, whereas fun f (x:t) means the argument type of f is t. There is no need to write result types (and in later assignments, no need to write argument types).
  3. fun (x t), fun (t x), or fun (t : x) are all wrong, but the error message suggests you are trying to do something much more advanced than you actually are (which is trying to write fun (x : t)).

Summary

Evaluating a correct homework solution should generate these bindings:

val is_older = fn : (int * int * int) * (int * int * int) -> bool
val number_in_month = fn : (int * int * int) list * int -> int
val number_in_months = fn : (int * int * int) list * int list -> int
val dates_in_month = fn : (int * int * int) list * int -> (int * int * int) list
val dates_in_months = fn : (int * int * int) list * int list -> (int * int * int) list
val get_nth = fn : string list * int -> string
val date_to_string = fn : int * int * int -> string
val number_before_reaching_sum = fn : int * int list -> int
val what_month = fn : int -> int
val month_range = fn : int * int -> int list
val oldest = fn : (int * int * int) list -> (int * int * int) option
val number_in_months_challenge = fn : (int * int * int) list * int list -> int
val dates_in_months_challenge = fn : (int * int * int) list * int list -> (int * int * int) list
val reasonable_date = fn : int * int * int -> bool

Of course, generating these bindings does not guarantee that your solutions are correct.

Test

source folder: src/test/sml/uw1
how to run with CM.make verbosity off: sml -Ccm.verbose=false run_uw1_testing.sml
how to run with CM.make verbosity on: sml run_uw1_testing.sml

note: ensure that you have removed all printing to receive credit for any assignment.

SML Error Messages

Test your functions. The unit tests you have been provided from UW are intentionally minimal.

Pledge, Acknowledgments, Citations

file: uw1-calendar-date-pledge-acknowledgments-citations.txt

More info about the Honor Pledge