Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; A student is:
- ;; (make-student string number string)
- (define-struct student (first last grade teacher))
- ;; CONSTRUCTOR
- ;; make-student: string number string -> student
- ;; SELECTORS
- ;; student-name: student -> string
- ;; student-grade: student -> number
- ;; student-teacher: student -> string
- ;; PREDICATE
- ;; student?: any -> boolean
- (define EMILY (make-student "Emily" "Stevens" 2 "Dominguez"))
- (define SAMANTHA (make-student "Samantha" "Dearing" 4 "Wilson"))
- (define GREG (make-student "Gregory" "Shoemaker" 1 "Grady"))
- ; promote: student string -> student
- ; given a student and his/her new teacher, produces a new student
- ; with the new teacher in the next grade
- (define (promote a-student new-teacher)
- (make-student (student-first a-student)
- (student-last a-student)
- (+ 1 (student-grade a-student))
- new-teacher))
- (check-expect (promote EMILY "Johnson") (make-student "Emily" "Stevens" 3 "Johnson"))
- (check-expect (promote SAMANTHA "Lightfoot") (make-student "Samantha" "Dearing" 5 "Lightfoot"))
- ; same-teacher?: student -> boolean
- ; given..., produces...
- (define (same-teacher? a-student b-student)
- (string=? (student-teacher a-student)(student-teacher b-student)))
- (check-expect (same-teacher? EMILY SAMANTHA) false)
- (check-expect (same-teacher? EMILY EMILY) true)
- ;; 1.
- ;; A person is:
- ;; (have-birthday string string number) <--- Data Definition
- (define-struct person (first last age))
- ;; CONSTRUCTOR
- ;; have-birthday: string string number -> person
- ;; SELECTORS
- ;; person-first: person -> string
- ;; person-last: person -> string
- ;; person-age: person -> number
- ;; PREDICATE
- ;; person?: any -> boolean
- (define SALLY (make-person "Sally" "Hanson" 28))
- (define JEFF (make-person "Jeff" "Henry" 16))
- (define CAITLIN (make-person "Caitlin" "Hill" 16))
- (define LOGAN (make-person "Logan" "Darling" 22))
- ;; have-birthday: person -> ?
- ;; given ..., produces ...
- (define (have-birthday a-person)
- (make-person (person-first a-person)
- (person-last a-person)
- (+ 1 (person-age a-person))))
- (check-expect (have-birthday SALLY) (make-person "Sally" "Hanson" 29))
- (check-expect (have-birthday LOGAN) (make-person "Logan" "Darling" 23))
- (check-expect (have-birthday CAITLIN) (make-person "Caitlin" "Hill" 17))
- ;; 2.
- ;; A book in a bookstore's inventory system has an author, a title, a section ("young adult", "computer", "science-fiction", etc.),
- ; and a price. When books aren't selling well, they get discounted. Write a function discount-book that consumes a book and returns
- ;; the same book with the price discounted 25%.
- ;; A book is:
- ;; (discount-book string string string number) <--- Data Definition
- (define-struct book (author title section price))
- ;; CONSTRUCTOR
- ;; make-book: string string string number -> book
- ;; SELECTORS
- ;; book-author: book -> string
- ;; book-title: book -> string
- ;; book-section: book -> string
- ;; book-price: book -> number
- ;; PREDICATE
- ;; book?: any -> boolean
- ;; discount-book: book -> ?
- ;; given ..., produces ...
- (define (discount-book a-book)
- (make-book (book-author a-book)
- (book-title a-book)
- (book-section a-book)
- (* 0.75 (book-price a-book))))
- (check-expect (discount-book HARRY-POTTER) (make-book "JK Rowling" "Harry Potter" "children" 18.7425))
- (check-expect (discount-book PRIDE-AND-PREJUDICE) (make-book "Jane Austin" "Pride and Prejudice" "romance" 11.9925 ))
- (check-expect (discount-book LORD-OF-THE-RINGS) (make-book "JR Tolkin" "Lord of the Rings" "fantasy" 14.9925))
- (define HARRY-POTTER (make-book "JK Rowling" "Harry Potter" "children" 24.99))
- (define PRIDE-AND-PREJUDICE (make-book "Jane Austin" "Pride and Prejudice" "romance" 15.99))
- (define LORD-OF-THE-RINGS (make-book "JR Tolkin" "Lord of the Rings" "fantasy" 19.99))
- ;; 3.
- ;; A class at a university has a department, a course number, a professor, and a number of credit hours.
- ;; Write the function same-department?. It consumes two classes and returns true if they're in the same department and false otherwise.
- ;; A class is:
- ;; (same-department? string number string number)
- (define-struct class (department course professor credit))
- ;; CONSTRUCTOR
- ;; same-department? : string number string number -> class
- ;; SELECTORS
- ;; class-department: class -> string
- ;; class-course: class -> number
- ;; class-professor: class -> string
- ;; class-credit: class -> number
- ;; PREDICATE
- ;; class?: any -> boolean
- ; same-department?: student -> ?
- ; given ..., produces ...
- (define (same-department? a-class b-class)
- (string=? (class-department a-class) (class-department b-class)))
- (check-expect (same-department? DRAMA CHEMISTRY) false)
- (check-expect (same-department? DRAWING CHEMISTRY) false)
- (check-expect (same-department? DRAWING DRAMA) true)
- (define DRAMA (make-class "arts" "drama" "teri" 1.0))
- (define CHEMISTRY (make-class "science" "chemistry" "dariff" 1.0))
- (define DRAWING (make-class "arts" "drawing" "gursh" 1.0))
- ;; 4.
- ;; CDs in a record store database have a title, artist, number of tracks, and total playing time. (To keep it easy, we'll assume the total time is in seconds.)
- ;; Write the function average-track-length which consumes a CD and returns the average length of each of its tracks in seconds.
- ;; A CD is:
- ;; (average-track-length? string string number number)
- (define-struct cd (title artist tracks time))
- ;; CONSTRUCTOR
- ;; average-track-length : string string number number -> class
- ;; SELECTORS
- ;; CD-title: CD -> string
- ;; CD-artist: CD -> string
- ;; CD-tracks: CD -> number
- ;; CD-time: CD -> number
- ;; PREDICATE
- ;; CD?: any -> boolean
- ; average-track-length? : CD -> ?
- ; given ..., produces ...
- ; (define (average-track-length? a-cd)
- ; (make-cd (cd-title a-cd)
- ; (cd-artist a-cd)
- ; ((/ (cd-time a-cd)
- ; (cd-tracks a-cd)))))
- ;
- ; (check-expect (average-track-length? ABBEY-ROAD) (make-cd "Abbey Road" "The Beatles" 192.5882353))
- ; (check-expect (average-track-length? THE-JOSHUA-TREE) (make-cd "The Joshua Tree" "U2" 273))
- ; (check-expect (average-track-length? FOR-EMMA-FOREVER-AGO) (make-cd "For Emma, Forever Ago" "Bon Iver" 255.5))
- ;
- ; (define ABBEY-ROAD (make-cd "Abbey Road" "The Beatles" 17 3274))
- ; (define THE-JOSHUA-TREE (make-cd "The Joshua Tree" "U2" 11 3003))
- ; (define FOR-EMMA-FOREVER-AGO (make-cd "For Emma, Forever Ago" "Bon Iver" 10 2555))
- ;
- ;; 5.
- ;; Hotel rooms in a certain chain are organized by number of beds, bed-size ("double", "queen", "king"), and base price per night.
- ;; The price of a hotel room goes up by $10 for every occupant over 2. Write the function compute-room-price which consumes a room and the number of occupants,
- ;; and produces the correct price.
- ;; A room is:
- ;; (compute-room-price number string number number)
- (define-struct room (beds bed-size base-price occupants ))
- ;; CONSTRUCTOR
- ;; compute-room-price : number string number -> room
- ;; SELECTORS
- ;; room-beds: room -> number
- ;; room-bed-size: room -> string
- ;; room-base-price: room -> number
- ;; PREDICATE
- ;; room?: any -> boolean
- ; compute-room-price: room -> ?
- ; given ..., produces ...
- (define (compute-room-price a-room)
- (make-room
- (room-beds a-room)
- (room-bed-size a-room)
- (+(*(- room-occupants 2)10)room-base-price a-student)))
- (check-expect (compute-room-price DELUXE 2) (make-room 2 "Queen" 89.99 ))
- (check-expect (compute-room-price BASIC 3) (make-room 1 "Queen" 79.99))
- (check-expect (compute-room-price DELUXE 4) (make-room 2 "Queen" 109.99))
- (define BASIC (make-room 1 "Queen" 69.99))
- (define DELUXE (make-room 2 "Queen" 89.99))
Add Comment
Please, Sign In to add comment