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)

1 Comments:

Anonymous Anonymous said...

NiN rocks! woo hoo.

10:31 PM  

Post a Comment

<< Home