Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table

This commit is contained in:
venomade committed 2026-11-22 11:15:35 +00:00
1 parent 77843402fa
commit 4675bd1106
76 files changed
+2505 -14

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
;;; CLOS 5: WITH-SLOTS AND WITH-ACCESSORS (solution)
(defclass point ()
((x :initarg :x :accessor point-x)
(y :initarg :y :accessor point-y)))
(defun double-point (p)
(with-slots (x y) p
(make-instance 'point :x (* x 2) :y (* y 2))))
(defun shift-x (p)
(with-accessors ((x point-x)) p
(setf x (1+ x)))
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))))
+29
View File
@@ -0,0 +1,29 @@
;;; CLOS 6: CALL-NEXT-METHOD AND NEXT-METHOD-P (solution)
(defclass animal () ())
(defclass dog (animal) ())
(defclass cat (animal) ())
(defgeneric announce (a))
(defmethod announce ((a animal))
"I am an animal.")
(defgeneric purr (a))
(defmethod announce ((d dog))
(concatenate 'string (call-next-method) " Woof!"))
(defmethod purr ((c cat))
(if (next-method-p)
(call-next-method)
"Prrr"))
;;; ------------------------------------------------------------------
;;; 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)))
+24
View File
@@ -0,0 +1,24 @@
;;; CLOS 7: PRINT-OBJECT AND DEFAULT-INITARGS (solution)
(defstruct candy filling size)
(defclass point ()
((x :initarg :x :accessor point-x)
(y :initarg :y :accessor point-y))
(:default-initargs :x 0 :y 0))
(defmethod print-object ((p point) stream)
(print-unreadable-object (p stream :type t)
(format stream "x=~a y=~a" (point-x p) (point-y p))))
;;; ------------------------------------------------------------------
;;; 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)))