#### Module 02

Last updated: Fri, 23 Jan 2015 17:43:09 -0500

##### Readings

HTDP/2e Part I, Chapters 4-7

##### Supplemental Materials

The Program Design Recipe, sections 1-5

Read up to (and including) section 5 this week. In particular, focus on more complex types of data (compound data and itemization data), templates, and design strategies.

Style Guide Familiarize yourself with the design recipe style expectations.

##### Pre-lecture

Read the Readings and Supplemental Materials.

##### Goals

Understand how to combine existing data definitions to form either compound data, or itemization data (or both).

Understand how the "shape" of a compound or itemization data definitions dictates the shape of a function that processes that kind of data (via the template).

Understand the difference between the function composition design strategy versus the data decomposition strategy, and how to write functions of each kind.

##### In-class

- Representing Time: Use BSL to implement these exercises. For some of the exercises, the remainder and quotient functions may be useful.
Come up with a data definition named Time that represents the number of seconds ellapsed since (the last) midnight. Then implement the following functions:

; time+ : Time Time -> Time ; Increments t1 by t2 amount of time ; STRATEGY: ? (define (time+ t1 t2) ...) ; time- : Time Time -> Time ; Decrements t1 by t2 amount of time ; STRATEGY: ? (define (time- t1 t2) ...) - Implement the following function, where Time.v2 can be either equivalent to your previous Time data definition or a completely new data definition:
Did a particular time representation make it easier to write some of the functions and harder to write some others?

Can you think of any other Time representations? How does it affect your function implementations?

Luggage Scanner: Come up with a solution to the luggage scanner problem.

##### In-class exercise 1 solution:

; Time Data Definition 1: ; A Time is a Natural, representing seconds since midnight.

; Time Data Definition 2: (define HOURS/DAY 24) (define MINS/HOUR 60) (define SECS/MIN 60) ; An Hours is a Natural in the interval [0,HOURS/DAY) ; A Minutes is a Natural in the interval [0,MINS/HOUR) ; A Seconds is a Natural in the interval [0,SECS/MIN) ; ; A Time is a (make-time Hours Minutes Seconds) ; Interp: represents a time of day, in 24hr format (define-struct time (hr min sec))

; Time Data Definition 3: ; A Time is a String, representing a time of day, ; WHERE: the string has the format: HH:MM:SS, ; where HH is a number in [0,24) representing hours ; MM is a number in [0,60) representing minutes ; and SS is a number in [0,60) representing seconds