Fix 14 wave-4 re-review findings (batches w4a+w4b)
This commit is contained in:
1 parent
a28a35991f
commit
fd5e11cdeb
23 files changed
+98
-29
No files matched your search
@@ -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
|
||||
|
||||
@@ -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.
|
||||
@@ -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,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
|
||||
|
||||
@@ -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,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.
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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))
|
||||
@@ -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.
|
||||
@@ -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))))
|
||||
|
||||
@@ -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,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.
|
||||
@@ -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)))
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
Reference in new issue
Block a user