Guest User

Untitled

a guest
Apr 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. ;1
  2.  
  3. (define-struct person (first last age))
  4.  
  5. ; ;; person-fun: person -> fun
  6. ; ;; given..., produces ...
  7. ; (define (person-fun a-person)
  8. ; (person-first a-person)
  9. ; (person-last a-person)
  10. ; (person-age a-person))
  11.  
  12.  
  13.  
  14. ; make-person: string string number
  15. ; person-first: person -> string
  16. ; person-last: person -> string
  17. ; person-age: person -> number
  18. ; person>: any -> boolean
  19.  
  20. (define HANNAH (make-person "Hannah" "Beach" 15))
  21. (define SHARZAUD (make-person "Sharzaud" "Karimi" 16))
  22. (define BEN (make-person "Ben" "Flanigan" 15))
  23.  
  24. ; happy-birthday: person -> person
  25. ; given a person with a givne age produces that person one year older
  26. (define (happy-birthday a-person)
  27. (make-person (person-first a-person)
  28. (person-last a-person)
  29. (+ 1 (person-age a-person))))
  30.  
  31. (check-expect (happy-birthday HANNAH) (make-person "Hannah" "Beach" 16))
  32. (check-expect (happy-birthday SHARZAUD) (make-person "Sharzaud" "Karimi" 17))
  33. (check-expect (happy-birthday BEN) (make-person "Ben" "Flanigan" 16))
  34.  
  35. ;2
  36.  
  37. (define-struct book (author title section price))
  38.  
  39. ; make-book: string string string number
  40. ; book-author: book -> string
  41. ; book-title: book -> string
  42. ; book-section: book -> string
  43. ; book-price: book -> number
  44.  
  45. (define WUTHERING (make-book "Bronte" "Wuthering Heights" "Romance" 10.00))
  46. (define PICTURING (make-book "Bloch" "Picturing Programs" "Non-fiction" 25.00))
  47. (define CHINESE (make-book "Sue-Mei Wu" "Chinese Links" "Language" 17.99))
  48.  
  49. ; discount-book: book -> book
  50. ; given a book with a given price produces on with a 25% price decrease
  51. (define (discount-book a-book)
  52. (make-book (book-author a-book)
  53. (book-title a-book)
  54. (book-section a-book)
  55. (* .75 (book-price a-book))))
  56.  
  57. (check-expect (discount-book WUTHERING) (make-book "Bronte" "Wuthering Heights" "Romance" 7.50))
  58. (check-expect (discount-book PICTURING) (make-book "Bloch" "Picturing Programs" "Non-fiction" (- 25 (* .25 25.00))))
  59. (check-expect (discount-book CHINESE) (make-book "Sue-Mei Wu" "Chinese Links" "Language" 13.4925))
  60.  
  61. ;3
  62.  
  63. (define-struct class (department coursenum professor numofcredhours))
  64.  
  65. ; make-class: string number string number
  66. ; class-department: class -> string
  67. ; class-coursenum: class -> number
  68. ; class-professor: class -> string
  69. ; class-numofcredhours: class -> number
  70. ; class?: any -> boolean
  71.  
  72. (define ALGEBRA (make-class "Math" 100 "O'Bryan" 15))
  73. (define CHEMISTRY (make-class "Science" 101 "Baar" 15))
  74. (define PHYSICS (make-class "Science" 102 "Murphy" 15))
  75.  
  76. ; same-department?: class class -> boolean
  77. ; given two classes produces whether they are in the same department or not
  78. (define (same-department? a-class b-class)
  79. (string=? (class-department a-class) (class-department b-class)))
  80.  
  81. (check-expect (same-department? ALGEBRA CHEMISTRY) false)
  82. (check-expect (same-department? CHEMISTRY PHYSICS) true)
  83. (check-expect (same-department? ALGEBRA PHYSICS) false)
  84.  
  85. ; 4.
  86. ;; A CD is:
  87. ;; (make-CD string string number number)
  88. (define-struct CD (title arist num-tracks playing-time))
  89.  
  90. ;; make-CD: string string number number -> CD
  91. ;; CD-title: CD -> string
  92. ;; CD-artist: CD -> string
  93. ;; CD-num-tracks: CD -> number
  94. ;; CD-playing-time: CD -> number
  95. ;; CD?: CD -> boolean
  96.  
  97. (define POWERSLAVE (make-CD "Powerslave" "Iron Maiden" 10 2500))
  98. (define BACK-IN-BLACK (make-CD "Back in Black" "AC/DC" 12 2700))
  99. (define BAD-MEETS-EVIL (make-CD "Welcome 2 Hell" "Bad Meets Evil" 15 2600))
  100.  
  101. ; ;; CD-fun: person -> ?
  102. ; ;; given ..., produces ...
  103. ; (define (CD-fun a-CD)
  104. ; (CD-title a-CD)
  105. ; (CD-artist a-CD)
  106. ; (CD-num-tracks a-CD)
  107. ; (CD-playing-time a-CD))
  108. ;
  109. ; (check-expect (CD-fun ...) ...)
  110. ; (check-expect (CD-fun ...) ...)
  111. ; (check-expect (CD-fun ...) ...)
  112.  
  113.  
  114. ;; average-track-length: CD -> number
  115. ;; given a CD, produces the average length of its tracks in seconds
  116. (define (average-track-length cd)
  117. (/ (CD-playing-time cd) (CD-num-tracks cd)))
  118.  
  119. (check-expect (average-track-length POWERSLAVE) 2500/10)
  120. (check-expect (average-track-length BACK-IN-BLACK) 2700/12)
  121. (check-expect (average-track-length BAD-MEETS-EVIL) 2600/15)
  122.  
  123. ;; 5
  124. ;; A room is:
  125. ;; (make-room number string number)
  126. (define-struct room (numbed bed-size base-price))
  127.  
  128. ;; Constructor
  129. ;; make-room: number string number -> room
  130.  
  131. ;; Selectors
  132. ;; room-numbed: room -> number
  133. ;; room-bed-size: room -> string
  134. ;; room-base-price: room -> number
  135.  
  136. ;; Predicate
  137. ;; room?: any -> boolean
  138.  
  139. ; ;; Template
  140. ; ;; room-fun: room -> ?
  141. ; ;; given... produces..
  142. ; (define (room-fun a-room)
  143. ; (room-numbed a-room)
  144. ; (room-bed-size a-room)
  145. ; (room-base-price a-room))
  146. ;
  147. ; (check-expect (room-fun ...) ...)
  148. ; (check-expect (room-fun ...) ...)
  149. ; (check-expect (room-fun ...) ...)
  150.  
  151.  
  152. (define RM-119 (make-room 2 "queen" 80))
  153. (define RM-223 (make-room 1 "king" 100))
  154. (define RM-334 (make-room 3 "queen" 120))
  155. (define RM-411 (make-room 2 "queen" 80))
  156.  
  157. ;; compute-room-price: room number -> number
  158. ;; given a room and number of occupants, produces a room price per night
  159. (define (compute-room-price a-room num-of-occupants)
  160. (cond
  161. [(<= num-of-occupants 2) (room-base-price a-room)]
  162. [else (+ (* 10 (- num-of-occupants 2))
  163. (room-base-price a-room))]))
  164.  
  165. (check-expect (compute-room-price RM-119 2) 80)
  166. (check-expect (compute-room-price RM-223 3) 110)
  167. (check-expect (compute-room-price RM-334 4) 140)
Add Comment
Please, Sign In to add comment