Fix 14 wave-4 re-review findings (batches w4a+w4b)

This commit is contained in:
venomade committed 2026-11-22 12:23:01 +00:00
1 parent a28a35991f
commit fd5e11cdeb
23 files changed
+98 -29

No files matched your search

+1 -1
View File
@@ -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
+2 -1
View File
@@ -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.
+1
View File
@@ -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.
+1
View File
@@ -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
+3 -2
View File
@@ -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.
+3 -1
View File
@@ -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.
+3 -2
View File
@@ -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
+2 -1
View File
@@ -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.
+1 -1
View File
@@ -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.
+3 -1
View File
@@ -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.
+11 -6
View File
@@ -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))
+3
View File
@@ -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.
+6 -2
View File
@@ -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))))
+12 -2
View File
@@ -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)))
+4 -1
View File
@@ -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.
+7
View File
@@ -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)))
+2 -1
View File
@@ -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.
+3 -1
View File
@@ -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.
+3 -3
View File
@@ -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.")
+2
View File
@@ -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))
+6 -2
View File
@@ -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))))
+12 -1
View File
@@ -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)))
+7
View File
@@ -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)))