Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;1
- (define-struct person (first last age))
- ; ;; person-fun: person -> fun
- ; ;; given..., produces ...
- ; (define (person-fun a-person)
- ; (person-first a-person)
- ; (person-last a-person)
- ; (person-age a-person))
- ; make-person: string string number
- ; person-first: person -> string
- ; person-last: person -> string
- ; person-age: person -> number
- ; person>: any -> boolean
- (define HANNAH (make-person "Hannah" "Beach" 15))
- (define SHARZAUD (make-person "Sharzaud" "Karimi" 16))
- (define BEN (make-person "Ben" "Flanigan" 15))
- ; happy-birthday: person -> person
- ; given a person with a givne age produces that person one year older
- (define (happy-birthday a-person)
- (make-person (person-first a-person)
- (person-last a-person)
- (+ 1 (person-age a-person))))
- (check-expect (happy-birthday HANNAH) (make-person "Hannah" "Beach" 16))
- (check-expect (happy-birthday SHARZAUD) (make-person "Sharzaud" "Karimi" 17))
- (check-expect (happy-birthday BEN) (make-person "Ben" "Flanigan" 16))
- ;2
- (define-struct book (author title section price))
- ; make-book: string string string number
- ; book-author: book -> string
- ; book-title: book -> string
- ; book-section: book -> string
- ; book-price: book -> number
- (define WUTHERING (make-book "Bronte" "Wuthering Heights" "Romance" 10.00))
- (define PICTURING (make-book "Bloch" "Picturing Programs" "Non-fiction" 25.00))
- (define CHINESE (make-book "Sue-Mei Wu" "Chinese Links" "Language" 17.99))
- ; discount-book: book -> book
- ; given a book with a given price produces on with a 25% price decrease
- (define (discount-book a-book)
- (make-book (book-author a-book)
- (book-title a-book)
- (book-section a-book)
- (* .75 (book-price a-book))))
- (check-expect (discount-book WUTHERING) (make-book "Bronte" "Wuthering Heights" "Romance" 7.50))
- (check-expect (discount-book PICTURING) (make-book "Bloch" "Picturing Programs" "Non-fiction" (- 25 (* .25 25.00))))
- (check-expect (discount-book CHINESE) (make-book "Sue-Mei Wu" "Chinese Links" "Language" 13.4925))
- ;3
- (define-struct class (department coursenum professor numofcredhours))
- ; make-class: string number string number
- ; class-department: class -> string
- ; class-coursenum: class -> number
- ; class-professor: class -> string
- ; class-numofcredhours: class -> number
- ; class?: any -> boolean
- (define ALGEBRA (make-class "Math" 100 "O'Bryan" 15))
- (define CHEMISTRY (make-class "Science" 101 "Baar" 15))
- (define PHYSICS (make-class "Science" 102 "Murphy" 15))
- ; same-department?: class class -> boolean
- ; given two classes produces whether they are in the same department or not
- (define (same-department? a-class b-class)
- (string=? (class-department a-class) (class-department b-class)))
- (check-expect (same-department? ALGEBRA CHEMISTRY) false)
- (check-expect (same-department? CHEMISTRY PHYSICS) true)
- (check-expect (same-department? ALGEBRA PHYSICS) false)
- ; 4.
- ;; A CD is:
- ;; (make-CD string string number number)
- (define-struct CD (title arist num-tracks playing-time))
- ;; make-CD: string string number number -> CD
- ;; CD-title: CD -> string
- ;; CD-artist: CD -> string
- ;; CD-num-tracks: CD -> number
- ;; CD-playing-time: CD -> number
- ;; CD?: CD -> boolean
- (define POWERSLAVE (make-CD "Powerslave" "Iron Maiden" 10 2500))
- (define BACK-IN-BLACK (make-CD "Back in Black" "AC/DC" 12 2700))
- (define BAD-MEETS-EVIL (make-CD "Welcome 2 Hell" "Bad Meets Evil" 15 2600))
- ; ;; CD-fun: person -> ?
- ; ;; given ..., produces ...
- ; (define (CD-fun a-CD)
- ; (CD-title a-CD)
- ; (CD-artist a-CD)
- ; (CD-num-tracks a-CD)
- ; (CD-playing-time a-CD))
- ;
- ; (check-expect (CD-fun ...) ...)
- ; (check-expect (CD-fun ...) ...)
- ; (check-expect (CD-fun ...) ...)
- ;; average-track-length: CD -> number
- ;; given a CD, produces the average length of its tracks in seconds
- (define (average-track-length cd)
- (/ (CD-playing-time cd) (CD-num-tracks cd)))
- (check-expect (average-track-length POWERSLAVE) 2500/10)
- (check-expect (average-track-length BACK-IN-BLACK) 2700/12)
- (check-expect (average-track-length BAD-MEETS-EVIL) 2600/15)
- ;; 5
- ;; A room is:
- ;; (make-room number string number)
- (define-struct room (numbed bed-size base-price))
- ;; Constructor
- ;; make-room: number string number -> room
- ;; Selectors
- ;; room-numbed: room -> number
- ;; room-bed-size: room -> string
- ;; room-base-price: room -> number
- ;; Predicate
- ;; room?: any -> boolean
- ; ;; Template
- ; ;; room-fun: room -> ?
- ; ;; given... produces..
- ; (define (room-fun a-room)
- ; (room-numbed a-room)
- ; (room-bed-size a-room)
- ; (room-base-price a-room))
- ;
- ; (check-expect (room-fun ...) ...)
- ; (check-expect (room-fun ...) ...)
- ; (check-expect (room-fun ...) ...)
- (define RM-119 (make-room 2 "queen" 80))
- (define RM-223 (make-room 1 "king" 100))
- (define RM-334 (make-room 3 "queen" 120))
- (define RM-411 (make-room 2 "queen" 80))
- ;; compute-room-price: room number -> number
- ;; given a room and number of occupants, produces a room price per night
- (define (compute-room-price a-room num-of-occupants)
- (cond
- [(<= num-of-occupants 2) (room-base-price a-room)]
- [else (+ (* 10 (- num-of-occupants 2))
- (room-base-price a-room))]))
- (check-expect (compute-room-price RM-119 2) 80)
- (check-expect (compute-room-price RM-223 3) 110)
- (check-expect (compute-room-price RM-334 4) 140)
Add Comment
Please, Sign In to add comment