\documentstyle[6.001-ps]{article}
\begin{document}
\header{6}{April 7, 1989}
\part{1: Homework exercises}
\exercise{6.1}
\vspace{3 in}
\exercise{6.2}
\vspace{3 in}
\part{2: The 6.001 Adventure Game}
\problem{6.1}
\bv
(define (make-thing name)
  (let ((n-o (make-named-object name))
        (owner 'no-one))
    (sequence
      (define self (lambda (message)
                     (cond ((eq? message ownable?) 
                            (lambda (self)
                              true))
                           ((eq? message owned?)
                            (lambda (self)
                              (not (eq? owner 'no-one))))
                           ((eq? message change-owner)
                            (lambda (self new-owner)
                              (set! owner new-owner)
                              'done))
                           ((eq? message owner)
                            (lambda (self)
                              owner))
                           (else
                            (get-method n-o message)))))
      self)))
\end{verbatim}
\problem{6.2}
\vspace{4 in}
\problem{6.3}
The objects \verb+pizza+ and \verb+bertucci-special+ are not the
same.  They both point to functions with completely independent
environments.  If \verb+HAL+ goes to a room with both objects in it,
he will print:
\par
\begin{quote}
\bv
At LOCATION : HAL says -- I see PIZZA PIZZA
\end{verbatim}
\end{quote}
This is because \verb+HAL+ gets the name by sending a \verb+name+
message to the object.  Both objects are created with the name
\verb+PIZZA+, even if they are named differently to the Scheme
interpreter.
\problem{6.4}
\bv
(defmethod (go-to-heaven)
  (for-each (lambda (item) (ask self 'lose item))
            possessions)
  (ask self 'say '("It is a far, far, better place I go to!"))
  (ask self 'move-to heaven)
  (remove-from-clock-list self)
  'dead)
\end{verbatim}
\vbreak
A given behavior for an object may always be implemented as a method.
A behavior can also be implemented as a procedure, given that behaviors
already exist to change any parts of the internal state of the object.
For example, if you wanted to create a behavior which reset the
restlessness of a person to zero, this would have to be implemented
as a method because the \verb+set!+ must be executed from within the
environment of the person.
\problem{6.5}
\verb+act+ \\
\verb+eat-person+ \\
\verb+person?+ \\
\verb+place+ \\
\verb+possessions+ \\
\verb+exits+ \\
\verb+move+ \\
\verb+say+ \\
\verb+look-around+ \\
\verb+take+ \\
\verb+lose+ \\
\verb+list-possessions+ \\
\verb+go+ \\
\verb+have-fit+ \\
\verb+move-to+ \\
\verb+name+
\problem{6.7}
\bv
(define marc (make-person 'marc dormitory 100))
;Value: MARC

(install-person marc)
;Value: INSTALLED

(define late-homework (make-thing 'late-homework))
;Value: LATE-HOMEWORK

(ask dormitory 'appear late-homework)
;Value: APPEARED

(ask (ask marc 'place) 'name)
;Value: DORMITORY

(ask (ask lou 'place) 'name)
;Value: LOU-OFFICE

(ask marc 'take late-homework)
At DORMITORY : MARC says -- I take LATE-HOMEWORK 

(clock)

(ask marc 'go 'east)
MARC moves from DORMITORY to BLDG-10 
;Value: MOVED

(clock)

(ask marc 'go 'north)
MARC moves from BLDG-10 to EGG-ATRIUM 
;Value: MOVED

(clock)
HAL moves from HAL-OFFICE to TECH-SQUARE 

(ask marc 'go 'east)
MARC moves from EGG-ATRIUM to BLDG-36 
;Value: MOVED

(clock)
LOU moves from LOU-OFFICE to COMPUTER-LAB 

(ask (ask grendel 'place) 'name)
;Value: DUNGEON

(ask marc 'go 'up)
MARC moves from BLDG-36 to COMPUTER-LAB 
At COMPUTER-LAB : MARC says -- Hi LOU 
;Value: MOVED

(clock)

(ask marc 'lose late-homework)
At COMPUTER-LAB : MARC says -- I lose LATE-HOMEWORK 

(clock)
GRENDEL moves from DUNGEON to EGG-ATRIUM 
HAL moves from TECH-SQUARE to HAL-OFFICE 

(ask lou 'take late-homework)
At COMPUTER-LAB : LOU says -- I take LATE-HOMEWORK 

(clock)

(ask marc 'say '("Now I can go to sleep!"))
At COMPUTER-LAB : MARC says -- Now I can go to sleep! 
\end{verbatim}
\problem{6.8}
\bv
(define (take-all person)
  (for-each (lambda (x) (if (is-a x 'ownable?)
                            (if (not (is-a x 'owned?))
                                (ask person 'take x)
                                '())
                            '()))
            (ask computer-lab 'things)))

(define foo (make-thing 'foo))
;Value: FOO

(ask computer-lab 'appear foo)
;Value: APPEARED

(define bar (make-thing 'bar))
;Value: BAR

(ask computer-lab 'appear bar)
;Value: APPEARED

(define baz (make-thing 'baz))
;Value: BAZ

(ask computer-lab 'appear baz)
;Value: APPEARED

(define quux (make-thing 'quux))
;Value: QUUX

(ask computer-lab 'appear quux)
;Value: APPEARED

(ask lou 'take bar)
At COMPUTER-LAB : LOU says -- I take BAR 

(ask marc 'take quux)
At COMPUTER-LAB : MARC says -- I take QUUX 

(ask (ask hal 'place) 'name)
;Value: HAL-OFFICE

(ask hal 'go 'down)
HAL moves from HAL-OFFICE to TECH-SQUARE 
;Value: MOVED

(ask hal 'go 'south)
HAL moves from TECH-SQUARE to BLDG-36 
;Value: MOVED

(ask hal 'go 'up)
HAL moves from BLDG-36 to COMPUTER-LAB 
At COMPUTER-LAB : HAL says -- Hi MARC LOU 
;Value: MOVED

(take-all hal)
At COMPUTER-LAB : HAL says -- I take BAZ 
At COMPUTER-LAB : HAL says -- I take FOO 

(mapcar (lambda (x) (ask x 'name)) (ask computer-lab 'things))
;Value: (HAL QUUX BAZ BAR FOO MARC LATE-HOMEWORK LOU)

(mapcar (lambda (x) (ask x 'list-possessions)) (list marc lou hal))
At COMPUTER-LAB : HAL says -- I have FOO BAZ 
At COMPUTER-LAB : LOU says -- I have BAR LATE-HOMEWORK 
At COMPUTER-LAB : MARC says -- I have QUUX 
;Value: ((*THE-NON-PRINTING-OBJECT*) (*THE-NON-PRINTING-OBJECT*) (*THE-NON-PRINTING-OBJECT*))
\end{verbatim}
\problem{6.9}
\bv
(define (make-beer)
  (let ((thing (make-thing 'beer)))
    (make-object
     (object-cond
      (defmethod (beer?) true)
      (else (get-method thing message))))))

(define bud (make-beer))
;Value: BUD

(is-a bud 'beer?)
;Value: #T
\end{verbatim}
\problem{6.10}
\bv
(define (make-dean name place threshold)
  (let ((p (make-person name place threshold)))
    (make-object
     (object-cond
      (defmethod (move-to new-place)
        (ask p 'move-to new-place)
        (for-each (lambda (x) (if (is-a x 'beer?)
                                  (sequence
                                    (ask self 'say '("I will not tolerate beer on campus!"))
                                    (smash-beer x (ask self 'place))
                                    (ask self 'say '("I have smashed the beer.")))
                                  '()))
                  (ask (ask self 'place) 'things)))
      (else (get-method p message))))))

(define (smash-beer b place)
  (ask marc 'lose b)
  (ask marc 'have-fit)
  (ask place 'gone b))


(define macbay (make-dean 'macbay dean-office 5))
;Value: MACBAY

(install-person macbay)
;Value: INSTALLED

(define marc (make-person 'marc dormitory 100))
;Value: MARC

(install-person marc)
;Value: INSTALLED

(define bud (make-beer))
;Value: BUD

(ask dormitory 'appear bud)
;Value: APPEARED

(ask marc 'take bud)
At DORMITORY : MARC says -- I take BEER 

(ask macbay 'go 'west)
MACBAY moves from DEAN-OFFICE to DORMITORY 
At DORMITORY : MACBAY says -- Hi MARC 
At DORMITORY : MACBAY says -- I will not tolerate beer on campus! 
At DORMITORY : MARC says -- I lose BEER 
At DORMITORY : MARC says -- Yaaaah! I am upset! 
At DORMITORY : MACBAY says -- I have smashed the beer. 
\end{verbatim}
\problem{6.11}
\bv
(define (make-party-troll name place threshold)
  (let ((troll (make-troll name place threshold)))
    (make-object
     (object-cond
      (defmethod (eat-person person)
        (if (is-beer-in (ask person 'possessions))
            (drink-beer (beer-in (ask person 'possessions))
                        (ask self 'place)
                        self)
            (ask troll 'eat-person person)))
      (else (get-method troll message))))))

(define (is-beer-in things)
  (cond ((null? things) '())
        ((is-a (car things) 'beer?))
        (else (is-beer-in (cdr things)))))

(define (beer-in things)
  (if (is-a (car things) 'beer?)
      (car things)
      (beer-in (cdr things))))

(define (drink-beer the-beer place troll)
  (ask troll 'say (list "I drank the beer!  Yum Yum!!"))
  (smash-beer the-beer place))

(define spuds (make-party-troll 'spuds EGG-Atrium 3))
;Value: SPUDS

(install-person spuds)
;Value: INSTALLED

(define marc (make-person 'marc dormitory 100))
;Value: MARC

(install-person marc)
;Value: INSTALLED

(define bud-light (make-beer))
;Value: BUD-LIGHT

(ask dormitory 'appear bud-light)
;Value: APPEARED

(ask marc 'take bud-light)
At DORMITORY : MARC says -- I take BEER 

(ask marc 'go 'east)
MARC moves from DORMITORY to BLDG-10 
;Value: MOVED

(ask spuds 'go 'south)
SPUDS moves from EGG-ATRIUM to BLDG-10 
At BLDG-10 : SPUDS says -- Hi MARC 
;Value: MOVED

(ask spuds 'act)
At BLDG-10 : SPUDS says -- I drank the beer!  Yum Yum!! 
At BLDG-10 : MARC says -- I lose BEER 
At BLDG-10 : MARC says -- Yaaaah! I am upset! 
;Value: DISAPPEARED

(ask spuds 'act)
At BLDG-10 : SPUDS says -- Growl.... I'm going to eat you, MARC 
At BLDG-10 : MARC says -- It is a far, far, better place I go to! 
MARC moves from BLDG-10 to HEAVEN 
At BLDG-10 : SPUDS says -- Chomp chomp. MARC tastes yummy! 
;Value: EATEN
\end{verbatim}
\problem{6.13 - extra credit}
The misquote is from chapter 15 of Charles Dicken's {\em A Tale of
Two Cities}, said by Sydney Carton on the scaffold as he is about to
be executed.  The quote should read:
\begin{quote}
It is a far, far better thing that I do, than I have ever done; it is a
far, far better rest that I go to, than I have ever known.
\end{quote}
\finally
I spent about eight hours total on this assignment, with about six
spend in lab.
\end{document}
