Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table
This commit is contained in:
1 parent
77843402fa
commit
4675bd1106
76 files changed
+2505
-14
No files matched your search
@@ -0,0 +1,37 @@
|
||||
;;; CLOS 5: WITH-SLOTS AND WITH-ACCESSORS
|
||||
;;;
|
||||
;;; Slot access through POINT-X and POINT-Y works, but spells the
|
||||
;;; name out every time. WITH-SLOTS reads the slots as plain
|
||||
;;; variables:
|
||||
;;;
|
||||
;;; (with-slots (x y) p (list x y))
|
||||
;;;
|
||||
;;; WITH-ACCESSORS does the same through the accessor names, so
|
||||
;;; SETF goes through the accessor too:
|
||||
;;;
|
||||
;;; (with-accessors ((x point-x)) p (setf x (1+ x)))
|
||||
|
||||
(defclass point ()
|
||||
((x :initarg :x :accessor point-x)
|
||||
(y :initarg :y :accessor point-y)))
|
||||
|
||||
;; TODO: The point P with both coordinates doubled - a NEW point.
|
||||
(defun double-point (p)
|
||||
(make-instance 'point :x (* 2 (point-x p)) :y (point-y p)))
|
||||
|
||||
;; TODO: P with its X stepped up by one - the SAME point, changed.
|
||||
(defun shift-x (p)
|
||||
(setf (point-x p) (+ 2 (point-x p)))
|
||||
p)
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((q (double-point (make-instance 'point :x 3 :y 4))))
|
||||
(and (eql 6 (point-x q)) (eql 8 (point-y q)))))
|
||||
(check (let ((p (shift-x (make-instance 'point :x 1))))
|
||||
(eql 2 (point-x p))))
|
||||
(check (let ((p (shift-x (make-instance 'point :x 10))))
|
||||
(eql 11 (point-x p))))
|
||||
@@ -0,0 +1,39 @@
|
||||
;;; CLOS 6: CALL-NEXT-METHOD AND NEXT-METHOD-P
|
||||
;;;
|
||||
;;; A method can stand on the one above it: CALL-NEXT-METHOD runs
|
||||
;;; the next most specific method and hands its answer back, so you
|
||||
;;; can build on it. When there is no next method, CALL-NEXT-METHOD
|
||||
;;; itself errors - NEXT-METHOD-P asks first.
|
||||
;;;
|
||||
;;; (defmethod speak ((d dog))
|
||||
;;; (concatenate 'string (call-next-method) " Woof!"))
|
||||
|
||||
(defclass animal () ())
|
||||
(defclass dog (animal) ())
|
||||
(defclass cat (animal) ())
|
||||
|
||||
(defgeneric announce (a))
|
||||
|
||||
(defmethod announce ((a animal))
|
||||
"I am an animal.")
|
||||
|
||||
(defgeneric purr (a))
|
||||
|
||||
;; TODO: A DOG announces what an animal announces, then adds
|
||||
;; " Woof!".
|
||||
(defmethod announce ((d dog))
|
||||
"Woof!")
|
||||
|
||||
;; TODO: CAT purrs: if a method above you exists, use its answer;
|
||||
;; otherwise say "Prrr".
|
||||
(defmethod purr ((c cat))
|
||||
(call-next-method))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "I am an animal. Woof!" (announce (make-instance 'dog)))
|
||||
(check-equal "I am an animal." (announce (make-instance 'animal)))
|
||||
(check-equal "Prrr" (purr (make-instance 'cat)))
|
||||
@@ -0,0 +1,36 @@
|
||||
;;; CLOS 7: PRINT-OBJECT AND DEFAULT-INITARGS
|
||||
;;;
|
||||
;;; DEFSTRUCT (chapter 12) writes the constructor, the readers and
|
||||
;;; the predicate for you. DEFCLASS spells them out by hand - and
|
||||
;;; buys methods, inheritance, and defaults:
|
||||
;;;
|
||||
;;; :INITFORM fills one slot when nothing else does.
|
||||
;;; :DEFAULT-INITARGS feeds an :INITARG its default, so
|
||||
;;; MAKE-INSTANCE can still override it.
|
||||
;;;
|
||||
;;; PRINT-OBJECT is the method the printer calls: define it, and
|
||||
;;; PRINC shows your objects your way. PRINT-UNREADABLE-OBJECT
|
||||
;;; builds the #<...> frame around your text.
|
||||
|
||||
(defstruct candy filling size)
|
||||
|
||||
;; TODO: POINT starts at the origin unless you say otherwise.
|
||||
(defclass point ()
|
||||
((x :initarg :x :accessor point-x)
|
||||
(y :initarg :y :accessor point-y)))
|
||||
|
||||
;; TODO: A point prints as #<POINT x=3 y=4>.
|
||||
(defmethod print-object ((p point) stream)
|
||||
(format stream "point"))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 'swirl (candy-filling (make-candy :filling 'swirl)))
|
||||
(check-equal 0 (point-x (make-instance 'point)))
|
||||
(check-equal 7 (point-x (make-instance 'point :x 7)))
|
||||
(check-equal "#<POINT x=3 y=4>"
|
||||
(with-output-to-string (s)
|
||||
(princ (make-instance 'point :x 3 :y 4) s)))
|
||||
Reference in new issue
Block a user