Tuesday, September 27, 2005

Solution to part 2 of today's lab

As promised, here is the code that the class developed during today's lab. Note that there are two structure definitions associated with Recording, but only one data definition! Solution code: (define-struct artist (first last))

;; An Artist is a (make-artist String String)



(define trent (make-artist "Trent" "Reznor"))

(define tori (make-artist "Tori" "Amos"))

(define paul (make-artist "Paul" "Barman"))



(define-struct cd (artist title num-discs))

(define-struct tape (artist title))

;; A Recording is either a:

;; - (make-cd Artist String Number)

;; - (make-tape Artist String)



(define downward-spiral

  (make-cd trent "The Downward Spiral" 1))

(define venus-and-back

  (make-cd tori "To Venus and Back Again" 2))

(define paullelujah

  (make-tape paul "Paullelujah"))



;; The below is the template for functions that process Recordings.

;; Note that despite knowing nothing about the purpose

;; of the function (thus the "????????" purpose statement), we

;; can still write down a considerable amount of code.

#|

;; ?process-recording : Recording -> ???

;; ????????

(define (?process-recording rec)

  (cond

    [(cd? rec) ... (cd-artist rec) ...

        ... (cd-title rec) ...

        ... (cd-num-discs rec) ...]

    [(tape? rec) ... (tape-artist rec) ...

        ... (tape-title rec) ...]))

|#



;; recording-price : Recording -> Number

;; return the price for album

(define (recording-price album)

  (cond

    [(cd? album) (* 11 (cd-num-discs album))]

    [(tape? album) 9]))



(equal? (recording-price downward-spiral)

      11)

(equal? (recording-price venus-and-back)

      22)

(equal? (recording-price paullelujah)

      9)

Monday, September 26, 2005

Lab 3: Structures

(The below was adapted from the staff's Lab 3 notes.)

1. Simple Structures

A bank has many programs that process information about the various accounts. For our purposes, an account has three pieces of information: the name of the account holder, the account number, and the amount of money in the account.

Exercise 1: Write the data definition and structure definition for a bank account. Write down the names of the constructor(s), predicate(s), and selector(s) introduced by the structure definition, and a contract for each. Finally, write three example bank accounts. Remember to give the example accounts names so you can use them again later.

Exercise 2: Design a function that consumes a bank account and a minimum balance and determines whether the account meets the balance requirement. Remember the design recipe! Use the stepper on some of your test cases to see how DrScheme evaluates the function.

Exercise 3: Design the function bigger-balance? that consumes two accounts and determines whether the first has a bigger balance than the second. Use the stepper to see how DrScheme evaluates this function as well.

Exercise 4 (optional): Design a function that consumes a bank account and produces a new account that is the same as the old one, except that it has earned 1% interest.


Quiz & Administrivia


2. Unions of Structures

(for solution, see below) A music store owner wants to write a program to manage the recordings in her inventory. Some of her recordings are on CDs, and the rest are on cassette tapes. A CD album has three properties:
  • an album title,
  • an artist, and
  • the number of discs in the album.
An audio tape has just two properties:
  • an album title and
  • an artist.
(For this exercise, we'll assume that no album requires more than one cassette, for simplicity's sake.) Further, an artist itself has two properties:
  • a first name and
  • a last name.

Exercise 5: Write a data definition and structure definitions for the recordings in the store. Construct examples! (Hint: if you don't have "A Recording is either a ..." written down somewhere, then you haven't finished the exercise!)

Exercise 6: Write the template for functions that process recordings.

Exercise 7: Write a function that computes the price of a single recording. Audio tapes are $9, and CDs cost $11 for each disc in the album.


3. Lots of Structures!

(watch!) (The above was adapted from the staff's Lab 3 notes.)

Tuesday, September 20, 2005

Lab 2 material

Notes for today's lab. (update; Ryan changed the lab notes to clarify the last exercise, and so the url had to be suitably modified.)

Monday, September 12, 2005

Useful Links for 1st Lab

  1. (Mandatory) Get a CCS account. The signup form is here.
  2. (Mandatory) Enable sharing of home directory between Windows and UNIX.
  3. (Mandatory) Install CSU211.plt file (called "the dot pee ell tee" file or "the handin plt file" or perhaps even the "see ess you two eleven plt file"). Note that you use this link from within DrScheme's "Install .plt File" menu item, rather than your web browser; see below: http://www.ccs.neu.edu/home/matthias/211-f05/csu211.plt
  4. (Optional) Visit your web server account.
UPDATE: Step 3 above is probably not happening during lab today, due to technical problems. Also, on the subject of "problems," here is a link to the section in HtDP on Errors.

Hello to everyone!

First lab is tomorrow. Looking forward to having a fun time assisting students as they teach themselves.