From fd5e11cdeb98afa1d7b8def852d287776124a7fb Mon Sep 17 00:00:00 2001 From: venomade Date: Sun, 22 Nov 2026 12:23:01 +0000 Subject: [PATCH] Fix 14 wave-4 re-review findings (batches w4a+w4b) --- exercises/01_sexps/README.md | 2 +- exercises/04_conditionals/README.md | 3 ++- exercises/05_predicates/pred4.lisp | 1 + exercises/06_lists/README.md | 1 + exercises/07_recursion/README.md | 5 +++-- exercises/10_loops/README.md | 4 +++- exercises/10_loops/loops6.lisp | 5 +++-- exercises/11_numbers/README.md | 3 ++- exercises/13_hash_tables/README.md | 2 +- exercises/14_packages/README.md | 4 +++- exercises/14_packages/pkg5.lisp | 17 +++++++++++------ exercises/15_clos/README.md | 3 +++ exercises/15_clos/clos5.lisp | 8 ++++++-- exercises/15_clos/clos6.lisp | 14 ++++++++++++-- exercises/16_conditions/README.md | 5 ++++- exercises/16_conditions/cond5.lisp | 7 +++++++ exercises/17_macros/README.md | 3 ++- exercises/18_dynamic/README.md | 4 +++- info.sexp | 6 +++--- solutions/14_packages/pkg5.lisp | 2 ++ solutions/15_clos/clos5.lisp | 8 ++++++-- solutions/15_clos/clos6.lisp | 13 ++++++++++++- solutions/16_conditions/cond5.lisp | 7 +++++++ 23 files changed, 98 insertions(+), 29 deletions(-) diff --git a/exercises/01_sexps/README.md b/exercises/01_sexps/README.md index d97e609..c2a50ae 100644 --- a/exercises/01_sexps/README.md +++ b/exercises/01_sexps/README.md @@ -1,6 +1,6 @@ # 01_sexps -S-expressions: prefix notation, nesting, and `quote`. +S-expressions: prefix notation, nesting, `quote`, and reader shorthand. Everything in Lisp is built from parenthesized forms with the operator first. No precedence rules, no ambiguity: the shape of the code is the diff --git a/exercises/04_conditionals/README.md b/exercises/04_conditionals/README.md index ea31e9f..852dc36 100644 --- a/exercises/04_conditionals/README.md +++ b/exercises/04_conditionals/README.md @@ -1,6 +1,7 @@ # 04_conditionals -IF, COND, WHEN, UNLESS, and the value-returning AND and OR. +IF, COND, WHEN, UNLESS, and the value-returning AND and OR. CASE and +ECASE dispatch on one value. NIL is the only false value. Zero and the empty string are true, and the empty list IS nil - watch for that one. diff --git a/exercises/05_predicates/pred4.lisp b/exercises/05_predicates/pred4.lisp index 9ba4314..eb4fdc3 100644 --- a/exercises/05_predicates/pred4.lisp +++ b/exercises/05_predicates/pred4.lisp @@ -6,6 +6,7 @@ ;;; too: (eql 1 1.0) is NIL. ;;; EQUALP the generous one: numbers by value - (equalp 1 1.0) is ;;; T - characters ignoring case. +;;; A character is written #\a - and #\A is another one. ;;; ;;; Fix each definition with the comparison its comment names. diff --git a/exercises/06_lists/README.md b/exercises/06_lists/README.md index ea15dc4..d4ac8d6 100644 --- a/exercises/06_lists/README.md +++ b/exercises/06_lists/README.md @@ -1,6 +1,7 @@ # 06_lists CONS cells, CAR and CDR, list building, association lists. +DESTRUCTURING-BIND binds a whole list of names at once. A list is a chain of pairs ending in NIL. Quote writes lists as data; LIST and CONS build them at runtime. Association lists map keys to diff --git a/exercises/07_recursion/README.md b/exercises/07_recursion/README.md index 750cd49..0938ab5 100644 --- a/exercises/07_recursion/README.md +++ b/exercises/07_recursion/README.md @@ -2,5 +2,6 @@ Lists and recursion grew up together. -The shape is always the same: a base case for NIL, a step that -consumes one element, and trust that the rest is already solved. +The shape is always the same: a base case, a shrinking step, and +trust that the rest is already solved. Lists lose an element at a +time, numbers count down - and sometimes two functions take turns. diff --git a/exercises/10_loops/README.md b/exercises/10_loops/README.md index 017143d..7e323ba 100644 --- a/exercises/10_loops/README.md +++ b/exercises/10_loops/README.md @@ -3,4 +3,6 @@ DOLIST, DOTIMES, and the LOOP macro. DOLIST and DOTIMES are simple walkers. LOOP is a composable -sublanguage: FOR ... COLLECT, SUM, WHEN, THEREIS. +sublanguage: FOR ... COLLECT, SUM, WHEN, THEREIS. DO and DO* spell +a loop out by hand, and LOOP answers with WITH, THEN, UNTIL, COUNT, +MAXIMIZE, and LOOP-FINISH for leaving early. diff --git a/exercises/10_loops/loops6.lisp b/exercises/10_loops/loops6.lisp index c948575..7f1806c 100644 --- a/exercises/10_loops/loops6.lisp +++ b/exercises/10_loops/loops6.lisp @@ -1,8 +1,9 @@ ;;; LOOPS 6: COUNT, MAXIMIZE, LOOP-FINISH ;;; ;;; LOOP accumulates more than lists: COUNT tallies the iterations -;;; whose test is true, MAXIMIZE tracks the biggest value seen (0 -;;; for an empty run, so WIDEST below answers NIL itself). +;;; whose test is true, MAXIMIZE tracks the biggest value seen - +;;; what an empty run answers is implementation-dependent (0 in +;;; SBCL), so WIDEST below handles the empty case itself. ;;; ;;; LOOP-FINISH leaves the iterations early, gently: the FINALLY ;;; clause still runs, so (finally (return total)) hands the value diff --git a/exercises/11_numbers/README.md b/exercises/11_numbers/README.md index 0543066..3399ae4 100644 --- a/exercises/11_numbers/README.md +++ b/exercises/11_numbers/README.md @@ -3,4 +3,5 @@ The number tower: integers, rationals, floats. Integers grow without limit and (/ 7 2) is the exact rational 7/2. -FLOOR, MOD and friends round and divide with exact rules. +FLOOR, MOD and friends round and divide with exact rules. DECLARE +promises a type to the compiler; CHECK-TYPE checks it live. diff --git a/exercises/13_hash_tables/README.md b/exercises/13_hash_tables/README.md index 925dcea..0601701 100644 --- a/exercises/13_hash_tables/README.md +++ b/exercises/13_hash_tables/README.md @@ -4,4 +4,4 @@ MAKE-HASH-TABLE, GETHASH, presence, and counting. GETHASH returns two values: the stored value, and whether the key exists at all. That second value is the honest answer to "is it -there?" +there?" MAPHASH walks every pair without you holding the keys. diff --git a/exercises/14_packages/README.md b/exercises/14_packages/README.md index 9bf3674..105f931 100644 --- a/exercises/14_packages/README.md +++ b/exercises/14_packages/README.md @@ -4,4 +4,6 @@ DEFPACKAGE, IN-PACKAGE, exports, and :USE. A package is a namespace. Only exported symbols are visible through a single colon; :USE imports them all, the way CL-USER uses -COMMON-LISP. +COMMON-LISP. FIND-SYMBOL looks a name up and INTERN makes-or-finds +it; :NICKNAMES give a package other names to answer to, and :SHADOW +claims a name before a USEd package can lend its own. diff --git a/exercises/14_packages/pkg5.lisp b/exercises/14_packages/pkg5.lisp index 4263669..8c3386c 100644 --- a/exercises/14_packages/pkg5.lisp +++ b/exercises/14_packages/pkg5.lisp @@ -2,10 +2,12 @@ ;;; ;;; A single colon reaches only EXPORTED symbols. The double colon ;;; shop::secret-sale reaches the internal ones too. :NICKNAMES -;;; gives a package extra names to call it by. And :SHADOW keeps a -;;; name a package USEs from colliding with your own - without it, -;;; defining FILL below would quietly redefine COMMON-LISP's FILL -;;; for everyone. +;;; gives a package extra names to call it by. And :SHADOW claims a +;;; name a USEd package would otherwise lend you: FILL below means +;;; POST-OFFICE's own symbol, the claim spelled out in the +;;; defpackage. Without it, plain FILL inside POST-OFFICE reads as +;;; COMMON-LISP's own - the DEFUN below would redefine that for +;;; everyone, and SBCL's package lock refuses, with an error. (defpackage #:shop (:use #:common-lisp) @@ -27,8 +29,9 @@ (shop:price)) ;; TODO: The defpackage below is missing its (:shadow #:fill). -;; Add it, or DEFUN FILL under POST-OFFICE redefines COMMON-LISP's -;; FILL for everyone. +;; Add it, so POST-OFFICE claims FILL up front - without the claim +;; the DEFUN below would redefine COMMON-LISP's own FILL, and +;; SBCL's package lock refuses to load the file at all. (defpackage #:post-office (:use #:common-lisp)) @@ -49,3 +52,5 @@ (check-equal (list 0 0 0) (fill (list 1 2 3) 0)) (check-equal '(post-office::stuffed crate) (funcall (find-symbol "FILL" '#:post-office) 'crate)) +(check (member "FILL" (package-shadowing-symbols '#:post-office) + :test #'string= :key #'symbol-name)) diff --git a/exercises/15_clos/README.md b/exercises/15_clos/README.md index 4db8a73..dbb8ffb 100644 --- a/exercises/15_clos/README.md +++ b/exercises/15_clos/README.md @@ -5,3 +5,6 @@ Classes, instances, generic dispatch, and method combination. DEFGENERIC names an operation; DEFMETHOD implements it, once per class. :BEFORE and :AFTER methods combine around the primary one, and (EQL x) specializers dispatch on a single value. +WITH-SLOTS and WITH-ACCESSORS read a whole object at a glance, +CALL-NEXT-METHOD stands on the method above, and PRINT-OBJECT with +:DEFAULT-INITARGS shape how objects print and build. diff --git a/exercises/15_clos/clos5.lisp b/exercises/15_clos/clos5.lisp index f74232e..1a73182 100644 --- a/exercises/15_clos/clos5.lisp +++ b/exercises/15_clos/clos5.lisp @@ -29,8 +29,12 @@ ;;; 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 (make-instance 'point :x 3 :y 4)) + (q (double-point p))) + (and (not (eq q p)) + (eql 3 (point-x p)) + (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)))) diff --git a/exercises/15_clos/clos6.lisp b/exercises/15_clos/clos6.lisp index 1888582..35d7c10 100644 --- a/exercises/15_clos/clos6.lisp +++ b/exercises/15_clos/clos6.lisp @@ -19,14 +19,23 @@ (defgeneric purr (a)) +(defmethod purr ((a animal)) + "prrr-from-animal") + +(defgeneric hiss (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". +;; otherwise say "Prrr" - and CAT hisses the same way, though HISS +;; has no method above CAT at all. (defmethod purr ((c cat)) + "Prrr") + +(defmethod hiss ((c cat)) (call-next-method)) ;;; ------------------------------------------------------------------ @@ -36,4 +45,5 @@ (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))) +(check-equal "prrr-from-animal" (purr (make-instance 'cat))) +(check-equal "Prrr" (hiss (make-instance 'cat))) diff --git a/exercises/16_conditions/README.md b/exercises/16_conditions/README.md index 9103a18..7253a07 100644 --- a/exercises/16_conditions/README.md +++ b/exercises/16_conditions/README.md @@ -4,4 +4,7 @@ Conditions, handlers, restarts: the crown jewel of Common Lisp. ERROR signals, HANDLER-CASE catches, UNWIND-PROTECT cleans up. DEFINE-CONDITION makes your own error types, and restarts let the -recovery happen exactly where it makes sense. +recovery happen exactly where it makes sense. :REPORT words a +condition for humans, WARN and SIGNAL carry on instead of +unwinding, restarts can take arguments, and IGNORE-ERRORS swaps an +error for NIL. diff --git a/exercises/16_conditions/cond5.lisp b/exercises/16_conditions/cond5.lisp index 5938a83..c36c439 100644 --- a/exercises/16_conditions/cond5.lisp +++ b/exercises/16_conditions/cond5.lisp @@ -46,8 +46,15 @@ (empty-cart (c) (format nil "~a" c)))) (check-equal '(battery 15) (battery-check 15)) (check-equal '(battery 90) (battery-check 90)) +(check-equal :noticed (handler-case (battery-check 15) + (warning () :noticed))) (check-equal :noticed (handler-case (warn "battery low") (warning () :noticed))) (check-equal :swapped (handler-case (check-battery 5) (low-battery () :swapped))) (check-equal '(battery 20) (check-battery 20)) +(check-equal :swapped (handler-case (check-battery 10) + (low-battery () :swapped))) +(check-equal '(battery 11) + (handler-case (check-battery 11) + (low-battery () :leaked))) diff --git a/exercises/17_macros/README.md b/exercises/17_macros/README.md index 59f1571..57899ec 100644 --- a/exercises/17_macros/README.md +++ b/exercises/17_macros/README.md @@ -4,4 +4,5 @@ DEFMACRO, backquote templates, hygiene with GENSYM. A macro writes code. The backquote family (` , ,@) makes templates cheap, and GENSYM keeps a macro's own variables from capturing -yours. +yours. The reader has macros too: SET-DISPATCH-MACRO-CHARACTER +teaches #\ a new trick, so #? can read as code of its own. diff --git a/exercises/18_dynamic/README.md b/exercises/18_dynamic/README.md index 7495253..430328c 100644 --- a/exercises/18_dynamic/README.md +++ b/exercises/18_dynamic/README.md @@ -4,4 +4,6 @@ Closures and special variables: two ways a function reaches out. A closure captures the variables around it. A DEFPARAMETER variable is dynamic: LET rebinding it changes what every called function -sees, and restores the old value afterwards. +sees, and restores the old value afterwards. DEFVAR makes the same +kind of variable but keeps quiet on reload - it never overwrites a +value that is already there. diff --git a/info.sexp b/info.sexp index ab1ff3e..6e884bc 100644 --- a/info.sexp +++ b/info.sexp @@ -190,7 +190,7 @@ Good luck - and keep a REPL open. That's the Lisp way!") :hint "FIND returns the element, POSITION its index, COUNT the matches: (position 'b '(a b c)) => 1. :test decides what matches; the default treats 1 and 1.0 as different, so use (find x xs :test #'=).") (:name "sequences2" :dir "19_sequences" - :hint "REMOVE drops matches: (remove x xs), mind the argument order. REMOVE-IF drops what its predicate accepts. SUBSTITUTE reads (NEW OLD SEQUENCE). EVERY wants the predicate true of all elements; SOME settles for one.") + :hint "REMOVE drops matches: (remove x xs), mind the argument order. REMOVE-IF drops what its predicate accepts. SUBSTITUTE reads (NEW OLD SEQUENCE).") (:name "sequences3" :dir "19_sequences" :hint "SORT takes the comparison first: (sort (list 3 1 2) #'<). With :key it compares one part of each element: (sort pairs #'< :key #'cdr). REVERSE returns a backwards copy - MAPCAR it over each word.") @@ -251,7 +251,7 @@ Good luck - and keep a REPL open. That's the Lisp way!") :hint "DO steps every variable with the OLD values, like LET - so TOTAL's step must be (+ total i). DO* steps one at a time like LET*, and B's step (* b a) then sees A's NEW value - swap the factorial's DO for DO* and it multiplies the right pairs.") (:name "loops6" :dir "10_loops" - :hint "COUNT wants a test: (loop for x in xs count (oddp x)). MAXIMIZE tracks the biggest: (loop for x in xs maximize x), NIL when the loop never runs. For the wallet LOOP-FINISH bails out before the total goes over: (if (> (+ total x) limit) (loop-finish) (setf total (+ total x))), and FINALLY returns the total either way.") + :hint "COUNT wants a test: (loop for x in xs count (oddp x)). MAXIMIZE tracks the biggest: (loop for x in xs maximize x) - but what an empty run answers is implementation-dependent (0 in SBCL), so WIDEST must answer NIL itself when XS is empty. For the wallet LOOP-FINISH bails out before the total goes over: (if (> (+ total x) limit) (loop-finish) (setf total (+ total x))), and FINALLY returns the total either way.") (:name "num1" :dir "11_numbers" :hint "Drop the 2.0: (expt 2 100) and (/ 7 2) stay exact when all arguments are integers.") @@ -302,7 +302,7 @@ Good luck - and keep a REPL open. That's the Lisp way!") :hint "Name the package: (find-symbol name 'attic) - with no package argument it searches *package* and finds nothing. INTERN makes-or-finds: (intern name 'attic) answers with the symbol either way, so (symbol-name (attic-intern \"SPARE-KEY\")) is \"SPARE-KEY\".") (:name "pkg5" :dir "14_packages" - :hint "The double colon reaches internals: shop::secret-sale. For the clash, add (:shadow #:fill) to the defpackage - then FILL inside POST-OFFICE means POST-OFFICE's own, and COMMON-LISP's FILL still fills sequences, which the third check relies on. The nickname STORE reaches SHOP the single-colon way.") + :hint "The double colon reaches internals: shop::secret-sale. Add (:shadow #:fill) to the defpackage: POST-OFFICE claims FILL as its own symbol up front, and the DEFUN below defines that one. Without the claim, plain FILL inside POST-OFFICE reads as COMMON-LISP's, and SBCL's package lock stops the file from loading. CL:FILL itself stays untouched - the third check still calls it from CL-USER. The nickname STORE reaches SHOP the single-colon way.") (:name "clos1" :dir "15_clos" :hint "MAKE-INSTANCE with initargs: (make-instance 'dog :name \"Rex\" :tricks '(\"sit\" \"roll-over\")). Initargs are keywords named after the slots.") diff --git a/solutions/14_packages/pkg5.lisp b/solutions/14_packages/pkg5.lisp index 4de3a3c..4cafa44 100644 --- a/solutions/14_packages/pkg5.lisp +++ b/solutions/14_packages/pkg5.lisp @@ -39,3 +39,5 @@ (check-equal (list 0 0 0) (fill (list 1 2 3) 0)) (check-equal '(post-office::stuffed crate) (funcall (find-symbol "FILL" '#:post-office) 'crate)) +(check (member "FILL" (package-shadowing-symbols '#:post-office) + :test #'string= :key #'symbol-name)) diff --git a/solutions/15_clos/clos5.lisp b/solutions/15_clos/clos5.lisp index db736e5..16c4a1c 100644 --- a/solutions/15_clos/clos5.lisp +++ b/solutions/15_clos/clos5.lisp @@ -18,8 +18,12 @@ ;;; 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 (make-instance 'point :x 3 :y 4)) + (q (double-point p))) + (and (not (eq q p)) + (eql 3 (point-x p)) + (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)))) diff --git a/solutions/15_clos/clos6.lisp b/solutions/15_clos/clos6.lisp index 69d866a..7903d82 100644 --- a/solutions/15_clos/clos6.lisp +++ b/solutions/15_clos/clos6.lisp @@ -11,6 +11,11 @@ (defgeneric purr (a)) +(defmethod purr ((a animal)) + "prrr-from-animal") + +(defgeneric hiss (a)) + (defmethod announce ((d dog)) (concatenate 'string (call-next-method) " Woof!")) @@ -19,6 +24,11 @@ (call-next-method) "Prrr")) +(defmethod hiss ((c cat)) + (if (next-method-p) + (call-next-method) + "Prrr")) + ;;; ------------------------------------------------------------------ ;;; Checks. Read them - they tell you what to aim for - but leave ;;; them unchanged. @@ -26,4 +36,5 @@ (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))) +(check-equal "prrr-from-animal" (purr (make-instance 'cat))) +(check-equal "Prrr" (hiss (make-instance 'cat))) diff --git a/solutions/16_conditions/cond5.lisp b/solutions/16_conditions/cond5.lisp index e199be9..0688c08 100644 --- a/solutions/16_conditions/cond5.lisp +++ b/solutions/16_conditions/cond5.lisp @@ -33,8 +33,15 @@ (empty-cart (c) (format nil "~a" c)))) (check-equal '(battery 15) (battery-check 15)) (check-equal '(battery 90) (battery-check 90)) +(check-equal :noticed (handler-case (battery-check 15) + (warning () :noticed))) (check-equal :noticed (handler-case (warn "battery low") (warning () :noticed))) (check-equal :swapped (handler-case (check-battery 5) (low-battery () :swapped))) (check-equal '(battery 20) (check-battery 20)) +(check-equal :swapped (handler-case (check-battery 10) + (low-battery () :swapped))) +(check-equal '(battery 11) + (handler-case (check-battery 11) + (low-battery () :leaked)))