Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.15 KB | None | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; Exercise 0 ;;
  3. ;; ;;
  4. ;; Create an object that every time it's called, flips its state ;;
  5. ;; between 0 and 1 ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8.  
  9. (define (make-flip)
  10. (let ((counter 0))
  11. (lambda ()
  12. (if ( = counter 0)
  13. (set! counter 1)
  14. (set! counter 0) )
  15. counter)
  16. ))
  17.  
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19. ;; Exercise 1 ;;
  20. ;; ;;
  21. ;; Install new character with free will, create late-homework object and ;;
  22. ;; start character in dormitory. ;;
  23. ;; ;;
  24. ;; Also create a function that finds gerry and gives him the homework ;;
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26.  
  27. (define myself
  28. (make&install-person 'myself dormitory 100))
  29.  
  30. (define late-homework
  31. (make&install-thing 'late-homework dormitory))
  32.  
  33. (define (give-gerry-my-homework)
  34. (ask myself 'take late-homework)
  35. (ask myself 'go 'west)
  36. (ask myself 'go 'north)
  37. (ask myself 'go 'up)
  38. (ask myself 'go 'up)
  39. (ask myself 'lose late-homework)
  40. (ask gerry 'take late-homework)
  41.  
  42.  
  43. )
  44.  
  45.  
  46. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  47. ;; Exercise 2 ;;
  48. ;; ;;
  49. ;; Creates a card locked place (overriding accept-person) ;;
  50. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  51. (define (check list thing)
  52. (cond ((null? list) #f)
  53. ((is-a (car list) thing) #t)
  54. ((check (cdr list) thing))
  55. )
  56. )
  57.  
  58. (define (make-card-locked-place name)
  59. (let ((place (make-place name)))
  60. (lambda (message)
  61. (cond ((eq? message 'accept-person?)
  62. ;; check if person holds a card
  63. (lambda (self person)
  64. (check (ask person 'possessions) 'sd-card? )
  65.  
  66.  
  67. )
  68. )
  69. (else (get-method place message))))))
  70.  
  71.  
  72. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  73. ;; Exercise 3 ;;
  74. ;; ;;
  75. ;; Create student-residence class that accepts only persons carrying ;;
  76. ;; cards with valid IDs ;;
  77. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  78. (define (check-list possessions thing ids)
  79. (cond ((null? possessions) #f)
  80. ((is-a (car possessions) thing)
  81. (if (eq? (car ids) (ask (car possessions) 'id))
  82. #t
  83. (check-list possessions thing (cdr ids))
  84. ))
  85. ((check-list (cdr possessions) thing ids))
  86. )
  87. )
  88.  
  89.  
  90. (define (check-list-for-value list value)
  91. (cond ((null? list) #f)
  92. ((eq? (car list) value) #t)
  93. (else (check-list-for-value (cdr list) value))
  94. )
  95. )
  96.  
  97. (define (look-for-correct-attribute-in-list items type attribute ids)
  98. (cond ((null? items) #f)
  99. ((is-a (car items) type)
  100. (if (check-list-for-value ids (ask (car items) attribute))
  101. #t
  102. (look-for-correct-attribute-in-list (cdr items) type attribute ids)
  103. )
  104. )
  105. (else (look-for-correct-attribute-in-list (cdr items) type attribute ids))
  106. )
  107. )
  108.  
  109. (define (append-item-in-list items value)
  110. (if (null? items)
  111. (list value)
  112. (append items (list value)))
  113. )
  114.  
  115. (define (make-student-residence name)
  116. (let ((place (make-card-locked-place name))
  117. (ids '()))
  118. (lambda (message)
  119. (cond ((eq? message 'accept-person?)
  120. (lambda (self person)
  121. (look-for-correct-attribute-in-list (ask person 'possessions) 'sd-card? 'id ids)
  122. )
  123. )
  124. ((eq? message 'register-card)
  125. (lambda (self card)
  126. (cond ((eq? (ask card 'place) self)
  127. (set! ids (append-item-in-list ids (ask card 'id)))
  128. #t)
  129. (else #f))
  130. )
  131. )
  132. ((eq? message 'ids)
  133. (lambda (self)
  134. ids
  135. )
  136. )
  137. (else (get-method place message))
  138. )
  139. )
  140. )
  141. )
  142.  
  143. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  144. ;; Exercise 4 ;;
  145. ;; ;;
  146. ;; Create ogre that hunts specific card id. ;;
  147. ;; Also create procedure that reports a stolen card and start an ogre ;;
  148. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  149. (define (pick-felon person possessions card-id)
  150. (cond
  151. ((null? possessions) #f)
  152. ((is-a (car possessions) 'sd-card?)
  153. (if (eq? (ask (car possessions) 'id) card-id) #t
  154. #f
  155. ))
  156. (else (pick-felon person (cdr possessions) card-id))
  157. ))
  158.  
  159.  
  160. (define (make-ogre name birthplace threshold card-id)
  161. (let ((person (make-person name birthplace threshold)))
  162. (lambda (message)
  163. (cond ((eq? message 'act)
  164. (lambda (self)
  165. (let ((others (other-people-at-place self (ask self 'place))))
  166. (define (search-and-eat-felons others card-id)
  167. (cond
  168. ((null? others) ((get-method person 'act) self))
  169. ((pick-felon (car others) (ask (car others) 'possessions) card-id) (ask self 'eat-person (car others)))
  170. (else (search-and-eat-felons (cdr others) card-id))
  171. ))
  172. (search-and-eat-felons others card-id)
  173. )))
  174. ((eq? message 'eat-person)
  175. (lambda (self person)
  176. (ask self 'say
  177. (list "Thief!!!!! I'm going to eat you,"
  178. (ask person 'name)))
  179. (go-to-heaven person)
  180. (ask self 'say
  181. (list "Chomp chomp." (ask person 'name)
  182. "tastes yummy!"))
  183. '*burp*))
  184. (else (get-method person message))))))
  185.  
  186. (define (make&install-ogre name birthplace threshold card-id)
  187. (let ((ogre (make-ogre name birthplace threshold card-id)))
  188. (ask ogre 'install)
  189. ogre))
  190.  
  191. (define (report-stolen-card id)
  192. (make&install-ogre (string->symbol (apply string-append (list "ogre" (symbol->string id)))) dungeon 1 id))
  193.  
  194.  
  195. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  196. ;; Exercise 5 ;;
  197. ;; ;;
  198. ;; Implement big-brother and surveillance-room ;;
  199. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  200. (define (find-id list)
  201. (cond ((null? list) #f)
  202. ((is-a (car list) 'sd-card?)
  203. (ask (car list) 'id))
  204. (else (find-id (cdr list)))
  205.  
  206. )
  207. )
  208.  
  209. (define (find-double list card-id place time)
  210. (cond ((null? list) #f)
  211. ((and (eq? (car (car list)) card-id) (eq? (cadr (car list)) place) (eq? (caddr (car list)) time)) #t)
  212. (else (find-double (cdr list) card-id place time))
  213. )
  214. )
  215.  
  216.  
  217. (define (make-big-brother name)
  218. (let ((named-obj (make-named-object name))
  219. (logs '())
  220. (stolen-cards '()))
  221. (lambda (message)
  222. (cond ((eq? message 'inform)
  223. (lambda (self place card-id)
  224. ;;check if stolen card
  225. (let ((time (current-time)))
  226. (if (find-double logs card-id place time)
  227. (begin
  228. (report-stolen-card card-id)
  229. (set! stolen-cards (append stolen-cards card-id))
  230. )
  231. (set! logs (append logs (list (list card-id place time))))
  232. ))
  233. ))
  234. ((eq? message 'display-stolen-card)
  235. (lambda (self) stolen-cards))
  236. (else (get-method named-obj message))))))
  237.  
  238. (define (make-surveillance-room name big-brother)
  239. (let ((student-residence (make-student-residence name)))
  240. (lambda (message)
  241. (cond ((eq? message 'accept-person?)
  242. (lambda (self person)
  243. (if (ask student-residence 'accept-person? person)
  244. (begin
  245. (ask big-brother 'inform self (find-id (ask person 'possessions)))
  246. #t)
  247. #f)
  248.  
  249. ))
  250. (else (get-method student-residence message))))))
  251.  
  252. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  253. ;; Exercise 6 ;;
  254. ;; ;;
  255. ;; Create class secret that extends thing and when taken opens a secret ;;
  256. ;; passage way between dormitory and Tech Square ;;
  257. ;; ;;
  258. ;; Also create object suspicious-book and install it in the dormitory ;;
  259. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  260.  
  261. (define (make-secret name birthplace)
  262. (let ((thing (make-thing name birthplace)))
  263. (lambda (message)
  264. (cond ((eq? message 'set-owner)
  265. (lambda (self new)
  266. (ask thing 'set-owner new)
  267. (can-go-both-ways dormitory 'north 'east Tech-Square)
  268. (ask new 'say (list "A new passage opens north!") )
  269. ))
  270. (else (get-method thing message))
  271. )
  272. )
  273. )
  274. )
  275.  
  276. (define (make&install-secret name birthplace)
  277. (let ((secret (make-secret name birthplace)))
  278. (ask secret 'install)
  279. secret))
  280.  
  281. (define suspicious-book
  282. (make&install-secret 'suspicious-book dormitory))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement