Guest User

Untitled

a guest
Apr 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. ;; A student is:
  2. ;; (make-student string number string)
  3. (define-struct student (first last grade teacher))
  4.  
  5. ;; CONSTRUCTOR
  6. ;; make-student: string number string -> student
  7.  
  8. ;; SELECTORS
  9. ;; student-name: student -> string
  10. ;; student-grade: student -> number
  11. ;; student-teacher: student -> string
  12.  
  13. ;; PREDICATE
  14. ;; student?: any -> boolean
  15.  
  16. (define EMILY (make-student "Emily" "Stevens" 2 "Dominguez"))
  17. (define SAMANTHA (make-student "Samantha" "Dearing" 4 "Wilson"))
  18. (define GREG (make-student "Gregory" "Shoemaker" 1 "Grady"))
  19.  
  20. ; promote: student string -> student
  21. ; given a student and his/her new teacher, produces a new student
  22. ; with the new teacher in the next grade
  23. (define (promote a-student new-teacher)
  24. (make-student (student-first a-student)
  25. (student-last a-student)
  26. (+ 1 (student-grade a-student))
  27. new-teacher))
  28.  
  29. (check-expect (promote EMILY "Johnson") (make-student "Emily" "Stevens" 3 "Johnson"))
  30. (check-expect (promote SAMANTHA "Lightfoot") (make-student "Samantha" "Dearing" 5 "Lightfoot"))
  31.  
  32.  
  33.  
  34.  
  35. ; same-teacher?: student -> boolean
  36. ; given..., produces...
  37. (define (same-teacher? a-student b-student)
  38. (string=? (student-teacher a-student)(student-teacher b-student)))
  39.  
  40. (check-expect (same-teacher? EMILY SAMANTHA) false)
  41. (check-expect (same-teacher? EMILY EMILY) true)
  42.  
  43.  
  44.  
  45. ;; 1.
  46.  
  47. ;; A person is:
  48. ;; (have-birthday string string number) <--- Data Definition
  49. (define-struct person (first last age))
  50.  
  51. ;; CONSTRUCTOR
  52. ;; have-birthday: string string number -> person
  53.  
  54. ;; SELECTORS
  55. ;; person-first: person -> string
  56. ;; person-last: person -> string
  57. ;; person-age: person -> number
  58.  
  59. ;; PREDICATE
  60. ;; person?: any -> boolean
  61.  
  62. (define SALLY (make-person "Sally" "Hanson" 28))
  63. (define JEFF (make-person "Jeff" "Henry" 16))
  64. (define CAITLIN (make-person "Caitlin" "Hill" 16))
  65. (define LOGAN (make-person "Logan" "Darling" 22))
  66.  
  67.  
  68. ;; have-birthday: person -> ?
  69. ;; given ..., produces ...
  70. (define (have-birthday a-person)
  71. (make-person (person-first a-person)
  72. (person-last a-person)
  73. (+ 1 (person-age a-person))))
  74.  
  75. (check-expect (have-birthday SALLY) (make-person "Sally" "Hanson" 29))
  76. (check-expect (have-birthday LOGAN) (make-person "Logan" "Darling" 23))
  77. (check-expect (have-birthday CAITLIN) (make-person "Caitlin" "Hill" 17))
  78.  
  79.  
  80. ;; 2.
  81. ;; A book in a bookstore's inventory system has an author, a title, a section ("young adult", "computer", "science-fiction", etc.),
  82. ; and a price. When books aren't selling well, they get discounted. Write a function discount-book that consumes a book and returns
  83. ;; the same book with the price discounted 25%.
  84.  
  85. ;; A book is:
  86. ;; (discount-book string string string number) <--- Data Definition
  87. (define-struct book (author title section price))
  88.  
  89. ;; CONSTRUCTOR
  90. ;; make-book: string string string number -> book
  91.  
  92. ;; SELECTORS
  93. ;; book-author: book -> string
  94. ;; book-title: book -> string
  95. ;; book-section: book -> string
  96. ;; book-price: book -> number
  97.  
  98. ;; PREDICATE
  99. ;; book?: any -> boolean
  100.  
  101. ;; discount-book: book -> ?
  102. ;; given ..., produces ...
  103. (define (discount-book a-book)
  104. (make-book (book-author a-book)
  105. (book-title a-book)
  106. (book-section a-book)
  107. (* 0.75 (book-price a-book))))
  108.  
  109. (check-expect (discount-book HARRY-POTTER) (make-book "JK Rowling" "Harry Potter" "children" 18.7425))
  110. (check-expect (discount-book PRIDE-AND-PREJUDICE) (make-book "Jane Austin" "Pride and Prejudice" "romance" 11.9925 ))
  111. (check-expect (discount-book LORD-OF-THE-RINGS) (make-book "JR Tolkin" "Lord of the Rings" "fantasy" 14.9925))
  112.  
  113. (define HARRY-POTTER (make-book "JK Rowling" "Harry Potter" "children" 24.99))
  114. (define PRIDE-AND-PREJUDICE (make-book "Jane Austin" "Pride and Prejudice" "romance" 15.99))
  115. (define LORD-OF-THE-RINGS (make-book "JR Tolkin" "Lord of the Rings" "fantasy" 19.99))
  116.  
  117.  
  118.  
  119.  
  120. ;; 3.
  121. ;; A class at a university has a department, a course number, a professor, and a number of credit hours.
  122. ;; Write the function same-department?. It consumes two classes and returns true if they're in the same department and false otherwise.
  123.  
  124. ;; A class is:
  125. ;; (same-department? string number string number)
  126. (define-struct class (department course professor credit))
  127.  
  128. ;; CONSTRUCTOR
  129. ;; same-department? : string number string number -> class
  130.  
  131. ;; SELECTORS
  132. ;; class-department: class -> string
  133. ;; class-course: class -> number
  134. ;; class-professor: class -> string
  135. ;; class-credit: class -> number
  136.  
  137. ;; PREDICATE
  138. ;; class?: any -> boolean
  139.  
  140. ; same-department?: student -> ?
  141. ; given ..., produces ...
  142. (define (same-department? a-class b-class)
  143. (string=? (class-department a-class) (class-department b-class)))
  144.  
  145.  
  146. (check-expect (same-department? DRAMA CHEMISTRY) false)
  147. (check-expect (same-department? DRAWING CHEMISTRY) false)
  148. (check-expect (same-department? DRAWING DRAMA) true)
  149.  
  150. (define DRAMA (make-class "arts" "drama" "teri" 1.0))
  151. (define CHEMISTRY (make-class "science" "chemistry" "dariff" 1.0))
  152. (define DRAWING (make-class "arts" "drawing" "gursh" 1.0))
  153.  
  154.  
  155. ;; 4.
  156. ;; 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.)
  157. ;; Write the function average-track-length which consumes a CD and returns the average length of each of its tracks in seconds.
  158.  
  159. ;; A CD is:
  160. ;; (average-track-length? string string number number)
  161. (define-struct cd (title artist tracks time))
  162.  
  163. ;; CONSTRUCTOR
  164. ;; average-track-length : string string number number -> class
  165.  
  166. ;; SELECTORS
  167. ;; CD-title: CD -> string
  168. ;; CD-artist: CD -> string
  169. ;; CD-tracks: CD -> number
  170. ;; CD-time: CD -> number
  171.  
  172. ;; PREDICATE
  173. ;; CD?: any -> boolean
  174.  
  175. ; average-track-length? : CD -> ?
  176. ; given ..., produces ...
  177. ; (define (average-track-length? a-cd)
  178. ; (make-cd (cd-title a-cd)
  179. ; (cd-artist a-cd)
  180. ; ((/ (cd-time a-cd)
  181. ; (cd-tracks a-cd)))))
  182. ;
  183. ; (check-expect (average-track-length? ABBEY-ROAD) (make-cd "Abbey Road" "The Beatles" 192.5882353))
  184. ; (check-expect (average-track-length? THE-JOSHUA-TREE) (make-cd "The Joshua Tree" "U2" 273))
  185. ; (check-expect (average-track-length? FOR-EMMA-FOREVER-AGO) (make-cd "For Emma, Forever Ago" "Bon Iver" 255.5))
  186. ;
  187. ; (define ABBEY-ROAD (make-cd "Abbey Road" "The Beatles" 17 3274))
  188. ; (define THE-JOSHUA-TREE (make-cd "The Joshua Tree" "U2" 11 3003))
  189. ; (define FOR-EMMA-FOREVER-AGO (make-cd "For Emma, Forever Ago" "Bon Iver" 10 2555))
  190. ;
  191.  
  192.  
  193. ;; 5.
  194. ;; Hotel rooms in a certain chain are organized by number of beds, bed-size ("double", "queen", "king"), and base price per night.
  195. ;; 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,
  196. ;; and produces the correct price.
  197.  
  198. ;; A room is:
  199. ;; (compute-room-price number string number number)
  200. (define-struct room (beds bed-size base-price occupants ))
  201.  
  202. ;; CONSTRUCTOR
  203. ;; compute-room-price : number string number -> room
  204.  
  205. ;; SELECTORS
  206. ;; room-beds: room -> number
  207. ;; room-bed-size: room -> string
  208. ;; room-base-price: room -> number
  209.  
  210.  
  211. ;; PREDICATE
  212. ;; room?: any -> boolean
  213.  
  214. ; compute-room-price: room -> ?
  215. ; given ..., produces ...
  216. (define (compute-room-price a-room)
  217. (make-room
  218. (room-beds a-room)
  219. (room-bed-size a-room)
  220. (+(*(- room-occupants 2)10)room-base-price a-student)))
  221.  
  222.  
  223. (check-expect (compute-room-price DELUXE 2) (make-room 2 "Queen" 89.99 ))
  224. (check-expect (compute-room-price BASIC 3) (make-room 1 "Queen" 79.99))
  225. (check-expect (compute-room-price DELUXE 4) (make-room 2 "Queen" 109.99))
  226.  
  227. (define BASIC (make-room 1 "Queen" 69.99))
  228. (define DELUXE (make-room 2 "Queen" 89.99))
Add Comment
Please, Sign In to add comment