8.12

Problem set 4: Composing functionsðŸ”—

This assignment is due on Wednesday, January 31 at 11:59pm. Submit it using Handin as assignment ps4. Corrections can be presented until Friday, February 23.

This problem set builds on the skills that we introduced in Lectures 2−6. To encourage you to review those lectures, your grade on this assignment will be capped by your grade on those lectures at the time you submit (or correct) this assignment. You can resubmit lecture exercises at any time.

Important Note: Remember to follow the design recipe whenever you design or write a function. In particular, every type mentioned in a signature must be introduced by a data definition, except for these well-known types: Number, Image, String, Color, Boolean.

1PreparationðŸ”—

In your textbook, turn to Chapter 2, Functions and Programs. Read Functions and Composing Functions.

You should also know how to use the design recipe with two kinds of data definitions, Enumerations and Intervals.

2Year, Month and DayðŸ”—

You will design functions that use dates as conventionally written in English. The following data definitions will be used throughout the problem set, and more will be introduced as they are needed.

 ; A Year is a non-negative integer ; Examples: ;   0 ;   1789 ;   2018 ; Non-examples: ;   -5000 ;   "AD 2018" ; A Month is one of: ; - "January" ; - "February" ; - "March" ; - "April" ; - "May" ; - "June" ; - "July" ; - "August" ; - "September" ; - "October" ; - "November" ; - "December" ; A Day is an integer at least 1 but at most 31 ; Examples: ;   1 ;   10 ;   31 ; Non-examples: ;   32 ;   "today"

Note that in general, it will be your job to create the data definitions. However, for this problem set we will provide the first steps of the design recipe, including the data definitions, so that you may focus on the subsequent steps in the design recipe.

In every problem set, clearly delimit your work for each exercise using a comment like “; Exercise 1”.

Exercise 1. Finish designing the following function.
 ; next-month : Month -> Month ; returns the month that comes after the given one (define (next-month m) ...) (check-expect (next-month "September") "October") (check-expect (next-month "December") "January")
Hint: your function template should make significant use of the Month data definition.

Exercise 2. Finish designing the following function.
 ; fall? : Month -> Boolean ; decides whether the given month is between September and November
Hint: your function template should make significant use of the Month data definition.

3calendarðŸ”—

The goal of this section is to design a program that generates an image displaying a given date. This goal is summarized by the following signature and purpose statement:
 ; calendar : Year Month Day -> Image ; returns an image of a date on a background
To keep the calendar function simple to define and easy to improve, we design helper functions that will be used in it.

Because there are several ways to write the same date, we make two more data definitions to represent a choice of date format:
 ; A MonthFormat is one of: ; - "long" ; - "short" ; A DateOrder is one of: ; - "MDY" ; - "DMY"

Exercise 3. Finish designing the following function.
 ; format-month : Month MonthFormat -> String ; abbreviates Month to three letters or not (define (format-month m f) ...) (check-expect (format-month "November" "long") "November") (check-expect (format-month "November" "short") "Nov")
Hint: your function template should make significant use of the MonthFormat data definition.

You will find the substring function useful here. Look it up in the documentation.

Exercise 4. Use format-month to design the following function:
 ; year-month-day->date : Year Month Day DateOrder MonthFormat -> String ; produces a date as a string ; given: 1936 "November" 12 "MDY" "long"   expect: "November 12, 1936" ; given: 1936 "November" 12 "MDY" "short"  expect: "Nov 12, 1936" ; given: 1936 "November" 12 "DMY" "long"   expect: "12 November 1936" ; given: 1936 "November" 12 "DMY" "short"  expect: "12 Nov 1936" (define (year-month-day->date y m d o f) ...)
Hint: your function template should make significant use of the DateOrder data definition.

You will find the built-in functions string-append and number->string useful. Look them up in the documentation.

For year-month-day->date and the other functions below, be sure to write unit tests with check-expect using the examples given (as well as any other examples you see fit to devise).

Exercise 5. Use year-month-day->date to design the calendar function, the goal of this section. You will need to choose one specific MonthFormat and one specific DateOrder that you like, as well as a background image to lay the text on. (If you are looking for some visual inspiration, do an image search online for the painter On Kawara.)

4days-betweenðŸ”—

The goal of this section is to design a program that calculates the number of days between two dates. As above, we decompose this task into subtasks to be carried out by helper functions.

To simplify, we pretend that every year has 365 days.

Analyzing this problem, we settle on the following approach. Given a date, we calculate how many days have elapsed since the earliest possible date (according to our definitions above): 1 Jan, 0. Let’s call this function year-month-day->days.
 ; year-month-day->days : Year Month Day -> Number ; returns the number of days elapsed since January 1, 0 ; given: 0 "January" 1     expect: 0 ; given: 2017 "August" 28  expect: 736444
Notice that year-month-day->days itself is not completely straightforward, because month lengths vary. Let us then tackle first the number of days before a given month.

Exercise 6. Finish designing the following function.
 ; month->days-in-year : Month -> Number ; returns the days elapsed in the year before the given month ; given: "January"    expect: 0 ; given: "September"  expect: 243
Hint: your function template should make significant use of the Month data definition.

Exercise 7. Finish designing year-month-day->days. Use month->days-in-year in the definition.

Hint: use the table method, like this:

Exercise 8. Design the function days-between, the goal of this section. This function should take two dates as input and calculate the number of days between them. Here is the signature:
 ; days-between : Year Month Day Year Month Day -> Number
Be sure to complete the purpose statement and devise examples/tests for days-between. The word “between” here means that there are 2 days between yesterday and tomorrow, not 1 or 3 days. With year-month-day->days in hand, the definition of days-between is a matter of basic arithmetic. Because days-between should return a non-negative integer, consider using the abs function.

5time-passingðŸ”—

In this section, the goal is to design a program which produces a simple animation of calendar. The animation will show the days of this semester passing, using animate.

Take a moment to reflect on this problem and where the difficulty lies. In a sense, we would like to feed calendar to animate, relax and watch the show. However, calendar takes three inputs, not a single number of days.

So we cannot simply compose animate and calendar. But we can design functions which allow us to connect the two. We need to turn a number of days into a Year, into a Month, and into a Day.

 ; days->year : Number -> Year ; takes days since 1 Jan 0 and returns the year ; given: 364                                       expect: 0 ; given: 365                                       expect: 1 ; given: 736305                                    expect: 2017 ; given: (year-month-day->days 1999 "December" 31) expect: 1999
The last example illustrates that days->year should be sort of an inverse to year-month-day->days.

Notice that days->year can be used to supply the Year to calendar. We also want days->month and days->day to supply the Month and Day to calendar. But these two functions are more challenging, because month lengths vary. So we provide the following data definitions, which are intervals:
 ; A DaysInYear is one of: ; - an integer at least   0 but less than  31 ; - an integer at least  31 but less than  59 ; - an integer at least  59 but less than  90 ; - an integer at least  90 but less than 120 ; - an integer at least 120 but less than 151 ; - an integer at least 151 but less than 181 ; - an integer at least 181 but less than 212 ; - an integer at least 212 but less than 243 ; - an integer at least 243 but less than 273 ; - an integer at least 273 but less than 304 ; - an integer at least 304 but less than 334 ; - an integer at least 334 but less than 365 ; *Interpretation*: the number of elapsed days ;                   since the first day of the year ; A DaysInMonth is an integer at least 0 but less than 31 ; *Interpretation*: The number of elapsed days ;                   since the first day of the month

Both days->month and days->day should be defined using helpers.

Exercise 10. Design the function days->month, by first designing the helper function days-in-year->month:
 ; days-in-year->month : DaysInYear -> Month ; takes days since the first of the year and returns the month ; given: 0    expect: "January" ; given: 31   expect: "February" ; given: 242  expect: "August" ; days->month : Number -> Month ; takes days since 1 Jan 0 and returns the month ; given: 59                                        expect: "March" ; given: 364                                       expect: "December" ; given: 736445                                    expect: "August" ; given: (year-month-day->days 1999 "December" 31) expect: "December"
Hint: Be sure that your template for days-in-year->month makes use of the DaysInYear data definition.

Exercise 11. Design the function days->day, by first designing the helper function days-in-year->days-in-month:
 ; days-in-year->days-in-month : DaysInYear -> DaysInMonth ; takes days since the first of the year ; and returns days since the first of the month ; given: 0       expect: 0 ; given: 59      expect: 0 ; given: 364     expect: 30 ; days->day : Number -> Day ; takes days since 1 Jan 0 and returns the day of the month ; given: 0                                         expect: 1 ; given: 59                                        expect: 1 ; given: 736324                                    expect: 30 ; given: (year-month-day->days 1999 "December" 31) expect: 31
Hint: To define days-in-year->days-in-month, even though the input DaysInYear is an enumeration, it is not necessary to use cond. Instead, compose the existing functions days-in-year->month and month->days-in-year.

Finally, we are ready to build our animation.

Exercise 12. Let’s create an animation which shows the days of this semester passing. The animation should start with the first day of this semester: January 8, 2024. Use year-month-day->days to define this constant:
 ; first-day : Number ; The number of days elapsed on January 8, 2024 since January 1, 0 (define first-day ...)

Then, finish designing semester-cal.
 ; A SemesterDay is a number ; of days since the first day of the semester ; semester-cal : SemesterDay -> Image ; takes a number of days since the first day of the semester ; and returns a calendar image of the corresponding date (define (semester-cal t) ...)

If you pass semester-cal to animate, you should see an animation that starts on January 8, 2024 and passes through the days quickly.

The calendar pages in this animation pass rather quickly: 28 per second to be exact. Let’s slow it down. Pick how many calendar pages per second you would like to see and define this number as cal-rate.

Finally, design a function semester which takes an animation tick number and produces an image by passing a SemesterDay to semester-cal. To convert the given animation tick number to a SemesterDay, multiply by cal-rate and then quotient by 28.

Your final program should produce a running animation using semester.

Help other students by answering this ungraded question: what did you have to learn to finish this problem set that we didn’t teach? Post your answer to Discord in the #ps4 channel, or put it as a comment at the bottom of your Handin submission.

Challenge Display weekend days (Saturdays and Sundays) differently from other days (e.g., by a different color of text or background). Also, freeze the animation when it gets to the last day of the semester.

Feel free to customize your calendar further. Can it display Spring Break differently, or alert you when a problem set is due?