;-*-Mode:LISP; Package: User; Base:10; Syntax:Common-lisp -*- ; ; ; ********************************************************************** ; Copyright (c) 1992, 1993 Xerox Corporation. ; All Rights Reserved. ; ; Use, reproduction, and preparation of derivative works are permitted. ; Any copy of this software or of any derivative work must include the ; above copyright notice of Xerox Corporation, this paragraph and the ; one after it. Any distribution of this software or derivative works ; must comply with all applicable United States export control laws. ; ; This software is made available AS IS, and XEROX CORPORATION DISCLAIMS ; ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE ; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ; PURPOSE, AND NOTWITHSTANDING ANY OTHER PROVISION CONTAINED HEREIN, ANY ; LIABILITY FOR DAMAGES RESULTING FROM THE SOFTWARE OR ITS USE IS ; EXPRESSLY DISCLAIMED, WHETHER ARISING IN CONTRACT, TORT (INCLUDING ; NEGLIGENCE) OR STRICT LIABILITY, EVEN IF XEROX CORPORATION IS ADVISED ; OF THE POSSIBILITY OF SUCH DAMAGES. ; ********************************************************************** ; ; ; (in-package :user) ; ; First, the politician program, so we'll have something to type at. ; ; (defclass () (concerns)) (defclass () (margin)) (defclass ( ) (state)) (defclass () ()) (defmethod initialize ((vip ) &key concerns) (slot-set vip 'concerns concerns)) (defmethod initialize ((vip ) &key margin) (slot-set vip 'margin margin)) (defmethod initialize ((vip ) &key state) (slot-set vip 'state state)) (defgeneric concerns (pol)) (defgeneric set-concerns (pol new)) (defgeneric margin (pol)) (defgeneric set-margin (pol new)) (defgeneric state (pol)) (defgeneric set-state (pol new)) (defgeneric vote (who issue)) (defmethod vote ((who ) issue) ;Cheap approximation of 'yes) ;do the popular thing (defmethod vote ((who ) issue) ;Cheap approximation of 'no) ;the cost-effective thing. ; ; III-14 ; Finding all Subclasses ; ; (defun subclasses (class) (remove-duplicates (cons class (mapappend #'subclasses (class-direct-subclasses class))))) (defun all-classes () (subclasses (find-class '))) ; ; III-19 ; Slot Geneology ; ; (defun slot-geneology (class slot-name) (slot-geneology-1 (class-cpl class) slot-name)) (defun slot-geneology-1 (cpl slot-name) (if (member slot-name (class-direct-slots (first cpl))) (class-name (first cpl)) (slot-geneology-1 (rest cpl) slot-name))) ; ; III-27 ; Average Number of Methods on GFs ; ; (defun all-gfs () (remove-duplicates (mapappend #'class-direct-gfs (all-classes)))) (defun class-direct-gfs (class) (mapcar #'method-gf (class-direct-methods class))) (defun all-methods () (mapappend #'class-direct-methods (all-classes))) ; ; III-30 ; Robot Complexity ; ; (defun robot-complexity-a (class gfs) (let ((methods (mapappend #'gf-methods gfs))) (count-if #'(lambda (m) (eq (method-specializer m) class)) methods))) (defun robot-complexity-b (class gfs) (let ((methods (mapappend #'gf-methods gfs))) (count-if #'(lambda (m) (member (method-specializer m) (class-cpl class))) methods))) ; ; III-50 ; Going to the Zoo ; ; (defclass ()()) (defclass () (long-necked tree-eating salt-licking)) (defclass () (soft coddly shy)) (defclass () (carnivorous predatory speedy)) (defclass ( ) ()) (defclass ( ) ()) (defun make-animals (class) (mapcar #'*make (&subclasses class))) ;(make-animals (find-class ')) --> 3 animals ; ; III-61 More Problems at the Zoo ; ; (defgeneric make-noise (animal)) (defmethod make-noise ((b )) (flet ((open-mouth ()) (generate-growl () (format t "Grrrrrr"))) (open-mouth) (generate-growl))) (defun display-behaviors (animal) (let* ((class (class-of animal)) (cpl (class-cpl class)) (possible-gfs (remove-duplicates (mapappend #'class-direct-gfs cpl))) (focused-gfs (remove-if-not #'(lambda (gf) (and (null (cdr (gf-arglist gf))) ;One argument gf? (some #'(lambda (m) (member (find-class ') (class-cpl (method-specializer m)))) (gf-methods gf)))) possible-gfs))) (mapcar #'(lambda (gf) (*apply-gf gf (list animal))) focused-gfs))) ;(display-behaviors (make )) --> The bear Growls... ; ; III-68 Buying a Car ; ; (defclass () (finish hue)) (defclass () (pigment-density)) (defclass () (paint-supplier)) (defclass