Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Exercise 0 ;;
- ;; ;;
- ;; Create an object that every time it's called, flips its state ;;
- ;; between 0 and 1 ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (define (make-flip)
- (let ((counter 0))
- (lambda ()
- (if ( = counter 0)
- (set! counter 1)
- (set! counter 0) )
- counter)
- ))
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Exercise 1 ;;
- ;; ;;
- ;; Install new character with free will, create late-homework object and ;;
- ;; start character in dormitory. ;;
- ;; ;;
- ;; Also create a function that finds gerry and gives him the homework ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (define myself
- (make&install-person 'myself dormitory 100))
- (define late-homework
- (make&install-thing 'late-homework dormitory))
- (define (give-gerry-my-homework)
- (ask myself 'take late-homework)
- (ask myself 'go 'west)
- (ask myself 'go 'north)
- (ask myself 'go 'up)
- (ask myself 'go 'up)
- (ask myself 'lose late-homework)
- (ask gerry 'take late-homework)
- )
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Exercise 2 ;;
- ;; ;;
- ;; Creates a card locked place (overriding accept-person) ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (define (check list thing)
- (cond ((null? list) #f)
- ((is-a (car list) thing) #t)
- ((check (cdr list) thing))
- )
- )
- (define (make-card-locked-place name)
- (let ((place (make-place name)))
- (lambda (message)
- (cond ((eq? message 'accept-person?)
- ;; check if person holds a card
- (lambda (self person)
- (check (ask person 'possessions) 'sd-card? )
- )
- )
- (else (get-method place message))))))
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Exercise 3 ;;
- ;; ;;
- ;; Create student-residence class that accepts only persons carrying ;;
- ;; cards with valid IDs ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (define (check-list possessions thing ids)
- (cond ((null? possessions) #f)
- ((is-a (car possessions) thing)
- (if (eq? (car ids) (ask (car possessions) 'id))
- #t
- (check-list possessions thing (cdr ids))
- ))
- ((check-list (cdr possessions) thing ids))
- )
- )
- (define (check-list-for-value list value)
- (cond ((null? list) #f)
- ((eq? (car list) value) #t)
- (else (check-list-for-value (cdr list) value))
- )
- )
- (define (look-for-correct-attribute-in-list items type attribute ids)
- (cond ((null? items) #f)
- ((is-a (car items) type)
- (if (check-list-for-value ids (ask (car items) attribute))
- #t
- (look-for-correct-attribute-in-list (cdr items) type attribute ids)
- )
- )
- (else (look-for-correct-attribute-in-list (cdr items) type attribute ids))
- )
- )
- (define (append-item-in-list items value)
- (if (null? items)
- (list value)
- (append items (list value)))
- )
- (define (make-student-residence name)
- (let ((place (make-card-locked-place name))
- (ids '()))
- (lambda (message)
- (cond ((eq? message 'accept-person?)
- (lambda (self person)
- (look-for-correct-attribute-in-list (ask person 'possessions) 'sd-card? 'id ids)
- )
- )
- ((eq? message 'register-card)
- (lambda (self card)
- (cond ((eq? (ask card 'place) self)
- (set! ids (append-item-in-list ids (ask card 'id)))
- #t)
- (else #f))
- )
- )
- ((eq? message 'ids)
- (lambda (self)
- ids
- )
- )
- (else (get-method place message))
- )
- )
- )
- )
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Exercise 4 ;;
- ;; ;;
- ;; Create ogre that hunts specific card id. ;;
- ;; Also create procedure that reports a stolen card and start an ogre ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (define (pick-felon person possessions card-id)
- (cond
- ((null? possessions) #f)
- ((is-a (car possessions) 'sd-card?)
- (if (eq? (ask (car possessions) 'id) card-id) #t
- #f
- ))
- (else (pick-felon person (cdr possessions) card-id))
- ))
- (define (make-ogre name birthplace threshold card-id)
- (let ((person (make-person name birthplace threshold)))
- (lambda (message)
- (cond ((eq? message 'act)
- (lambda (self)
- (let ((others (other-people-at-place self (ask self 'place))))
- (define (search-and-eat-felons others card-id)
- (cond
- ((null? others) ((get-method person 'act) self))
- ((pick-felon (car others) (ask (car others) 'possessions) card-id) (ask self 'eat-person (car others)))
- (else (search-and-eat-felons (cdr others) card-id))
- ))
- (search-and-eat-felons others card-id)
- )))
- ((eq? message 'eat-person)
- (lambda (self person)
- (ask self 'say
- (list "Thief!!!!! I'm going to eat you,"
- (ask person 'name)))
- (go-to-heaven person)
- (ask self 'say
- (list "Chomp chomp." (ask person 'name)
- "tastes yummy!"))
- '*burp*))
- (else (get-method person message))))))
- (define (make&install-ogre name birthplace threshold card-id)
- (let ((ogre (make-ogre name birthplace threshold card-id)))
- (ask ogre 'install)
- ogre))
- (define (report-stolen-card id)
- (make&install-ogre (string->symbol (apply string-append (list "ogre" (symbol->string id)))) dungeon 1 id))
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Exercise 5 ;;
- ;; ;;
- ;; Implement big-brother and surveillance-room ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (define (find-id list)
- (cond ((null? list) #f)
- ((is-a (car list) 'sd-card?)
- (ask (car list) 'id))
- (else (find-id (cdr list)))
- )
- )
- (define (find-double list card-id place time)
- (cond ((null? list) #f)
- ((and (eq? (car (car list)) card-id) (eq? (cadr (car list)) place) (eq? (caddr (car list)) time)) #t)
- (else (find-double (cdr list) card-id place time))
- )
- )
- (define (make-big-brother name)
- (let ((named-obj (make-named-object name))
- (logs '())
- (stolen-cards '()))
- (lambda (message)
- (cond ((eq? message 'inform)
- (lambda (self place card-id)
- ;;check if stolen card
- (let ((time (current-time)))
- (if (find-double logs card-id place time)
- (begin
- (report-stolen-card card-id)
- (set! stolen-cards (append stolen-cards card-id))
- )
- (set! logs (append logs (list (list card-id place time))))
- ))
- ))
- ((eq? message 'display-stolen-card)
- (lambda (self) stolen-cards))
- (else (get-method named-obj message))))))
- (define (make-surveillance-room name big-brother)
- (let ((student-residence (make-student-residence name)))
- (lambda (message)
- (cond ((eq? message 'accept-person?)
- (lambda (self person)
- (if (ask student-residence 'accept-person? person)
- (begin
- (ask big-brother 'inform self (find-id (ask person 'possessions)))
- #t)
- #f)
- ))
- (else (get-method student-residence message))))))
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Exercise 6 ;;
- ;; ;;
- ;; Create class secret that extends thing and when taken opens a secret ;;
- ;; passage way between dormitory and Tech Square ;;
- ;; ;;
- ;; Also create object suspicious-book and install it in the dormitory ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (define (make-secret name birthplace)
- (let ((thing (make-thing name birthplace)))
- (lambda (message)
- (cond ((eq? message 'set-owner)
- (lambda (self new)
- (ask thing 'set-owner new)
- (can-go-both-ways dormitory 'north 'east Tech-Square)
- (ask new 'say (list "A new passage opens north!") )
- ))
- (else (get-method thing message))
- )
- )
- )
- )
- (define (make&install-secret name birthplace)
- (let ((secret (make-secret name birthplace)))
- (ask secret 'install)
- secret))
- (define suspicious-book
- (make&install-secret 'suspicious-book dormitory))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement