From 6e2c382c78b01f6d5475ea30ad5cc8b4b72f6578 Mon Sep 17 00:00:00 2001 From: venomade Date: Sun, 22 Nov 2026 04:52:35 +0000 Subject: [PATCH] Fix 16 mid-chapter exercise defects (scouts mid1+mid2) Verified: dev/verify.sh all 70 exercises green (broken fail, solved pass). --- exercises/04_conditionals/if1.lisp | 1 + exercises/04_conditionals/if3.lisp | 1 + exercises/05_predicates/pred1.lisp | 2 ++ exercises/05_predicates/pred3.lisp | 1 + exercises/06_lists/lists5.lisp | 4 ++-- exercises/07_recursion/recurs4.lisp | 1 + exercises/08_functional/func2.lisp | 2 ++ exercises/08_functional/func3.lisp | 1 + exercises/08_functional/func4.lisp | 1 + exercises/09_strings/str1.lisp | 6 +++--- exercises/11_numbers/num2.lisp | 1 + exercises/12_structs/structs1.lisp | 2 +- exercises/12_structs/structs3.lisp | 4 ++-- exercises/13_hash_tables/hash3.lisp | 1 + info.sexp | 4 ++-- solutions/04_conditionals/if1.lisp | 1 + solutions/04_conditionals/if3.lisp | 1 + solutions/05_predicates/pred1.lisp | 2 ++ solutions/05_predicates/pred3.lisp | 1 + solutions/06_lists/lists5.lisp | 4 ++-- solutions/08_functional/func4.lisp | 1 + solutions/10_loops/loops1.lisp | 2 +- solutions/11_numbers/num2.lisp | 1 + solutions/12_structs/structs3.lisp | 4 ++-- 24 files changed, 34 insertions(+), 15 deletions(-) diff --git a/exercises/04_conditionals/if1.lisp b/exercises/04_conditionals/if1.lisp index 2ef34bc..8fd0292 100644 --- a/exercises/04_conditionals/if1.lisp +++ b/exercises/04_conditionals/if1.lisp @@ -20,3 +20,4 @@ (check-equal "hot" (classify 35)) (check-equal "pleasant" (classify 20)) (check-equal "hot" (classify 31)) +(check-equal "pleasant" (classify 30)) diff --git a/exercises/04_conditionals/if3.lisp b/exercises/04_conditionals/if3.lisp index 04f27b2..eb4f37f 100644 --- a/exercises/04_conditionals/if3.lisp +++ b/exercises/04_conditionals/if3.lisp @@ -21,5 +21,6 @@ (check-equal nil (only-positive-squares -3)) (check-equal 9 (only-positive-squares 3)) +(check-equal nil (only-positive-squares 0)) (check-equal :welcome (enter t)) (check-equal :denied (enter nil)) diff --git a/exercises/05_predicates/pred1.lisp b/exercises/05_predicates/pred1.lisp index 0da7218..9752884 100644 --- a/exercises/05_predicates/pred1.lisp +++ b/exercises/05_predicates/pred1.lisp @@ -16,3 +16,5 @@ (check (truthy-p "")) (check (not (truthy-p nil))) (check (not (truthy-p '()))) +(check-equal t (truthy-p 0)) +(check-equal nil (truthy-p nil)) diff --git a/exercises/05_predicates/pred3.lisp b/exercises/05_predicates/pred3.lisp index 9f38938..2fbf173 100644 --- a/exercises/05_predicates/pred3.lisp +++ b/exercises/05_predicates/pred3.lisp @@ -22,3 +22,4 @@ (check-equal :string (describe-value "hi")) (check-equal :list (describe-value '(a b))) (check-equal :nothing (describe-value nil)) +(check-equal :other (describe-value 'hi)) diff --git a/exercises/06_lists/lists5.lisp b/exercises/06_lists/lists5.lisp index 4f40f5a..e8a5d9e 100644 --- a/exercises/06_lists/lists5.lisp +++ b/exercises/06_lists/lists5.lisp @@ -25,5 +25,5 @@ (check-equal 2 (price 'tea)) (check-equal nil (price 'chai)) (check (let ((menu (add-item 'chai 5))) - (and (eql 5 (price 'chai)) - (eql 3 (price 'coffee))))) + (and (eql 5 (cdr (assoc 'chai menu))) + (eql 3 (cdr (assoc 'coffee menu)))))) diff --git a/exercises/07_recursion/recurs4.lisp b/exercises/07_recursion/recurs4.lisp index 473358f..3fbcae8 100644 --- a/exercises/07_recursion/recurs4.lisp +++ b/exercises/07_recursion/recurs4.lisp @@ -1,5 +1,6 @@ ;;; RECURSION 4: WALKING A TREE ;;; +;;; ATOM is T for anything that is not a cons cell. ;;; Lists nest, and a tree has TWO recursive parts: the first ;;; element and the rest. Recurse into both. diff --git a/exercises/08_functional/func2.lisp b/exercises/08_functional/func2.lisp index f3958e9..3aedcdc 100644 --- a/exercises/08_functional/func2.lisp +++ b/exercises/08_functional/func2.lisp @@ -3,6 +3,8 @@ ;;; MAPCAR applies a function to each element and collects the ;;; results into a new list: ;;; (mapcar #'1+ '(1 2 3)) => (2 3 4) +;;; Uppercase a string with STRING-UPCASE: (string-upcase "ada") +;;; => "ADA" (chapter 09 digs in). ;; TODO: Every element of XS, doubled. (defun double-all (xs) diff --git a/exercises/08_functional/func3.lisp b/exercises/08_functional/func3.lisp index e3ac70f..749c966 100644 --- a/exercises/08_functional/func3.lisp +++ b/exercises/08_functional/func3.lisp @@ -3,6 +3,7 @@ ;;; FUNCALL spells the arguments out; APPLY takes the last of them ;;; from a list: ;;; (apply #'+ 1 2 '(3 4)) => 10 +;;; MAX returns the largest of its arguments: (max 1 5 2) => 5 ;; TODO: Add up all of XS, using APPLY. (defun sum-of (xs) diff --git a/exercises/08_functional/func4.lisp b/exercises/08_functional/func4.lisp index 82753d8..7841cbf 100644 --- a/exercises/08_functional/func4.lisp +++ b/exercises/08_functional/func4.lisp @@ -19,6 +19,7 @@ (check-equal 4 (biggest '(3 1 4))) (check-equal 2 (biggest '(2))) +(check-equal 9 (biggest '(1 9 2))) (check-equal 6 (product '(1 2 3))) (check-equal 7 (product '(7))) (check-equal 1 (product '())) diff --git a/exercises/09_strings/str1.lisp b/exercises/09_strings/str1.lisp index 4fc4d63..1267063 100644 --- a/exercises/09_strings/str1.lisp +++ b/exercises/09_strings/str1.lisp @@ -1,8 +1,8 @@ ;;; STRINGS 1: FORMAT DIRECTIVES ;;; -;;; FORMAT is a little language: ~a prints readably, ~10a pads to a -;;; width, ~% is a newline. The first argument NIL means "return a -;;; string" instead of printing. +;;; FORMAT is a little language: ~a prints a value plainly; ~s +;;; prints it readably. ~10a pads to a width, ~% is a newline. The +;;; first argument NIL means "return a string" instead of printing. ;;; (format nil "Hello, ~a!" "Ada") => "Hello, Ada!" ;; TODO: "Ada has 42 cats." with the name and count in place. diff --git a/exercises/11_numbers/num2.lisp b/exercises/11_numbers/num2.lisp index 0e888f7..3cb2c94 100644 --- a/exercises/11_numbers/num2.lisp +++ b/exercises/11_numbers/num2.lisp @@ -20,6 +20,7 @@ (check-equal 10 (floor-to-five 12)) (check-equal 10 (floor-to-five 14)) (check-equal 15 (floor-to-five 15)) +(check-equal -15 (floor-to-five -12)) (check-equal 4 (mod-five 9)) (check-equal 0 (mod-five -5)) (check-equal 1 (mod-five -9)) diff --git a/exercises/12_structs/structs1.lisp b/exercises/12_structs/structs1.lisp index 0d29556..fc6d314 100644 --- a/exercises/12_structs/structs1.lisp +++ b/exercises/12_structs/structs1.lisp @@ -2,7 +2,7 @@ ;;; ;;; DEFSTRUCT defines a record type and generates, for free: ;;; a constructor (make-person), slot readers (person-name), -;;; and a predicate (personp). +;;; and a predicate (person-p). (defstruct person name diff --git a/exercises/12_structs/structs3.lisp b/exercises/12_structs/structs3.lisp index 802caf3..95760e5 100644 --- a/exercises/12_structs/structs3.lisp +++ b/exercises/12_structs/structs3.lisp @@ -19,9 +19,9 @@ ;;; them unchanged. ;;; ------------------------------------------------------------------ -(check (let ((c (make-counter))) +(check (let ((c (make-counter :value 9))) (bump c) - (eql 1 (counter-value c)))) + (eql 10 (counter-value c)))) (check (let ((c (make-counter :value 9))) (reset-counter c) (eql 0 (counter-value c)))) diff --git a/exercises/13_hash_tables/hash3.lisp b/exercises/13_hash_tables/hash3.lisp index 222d7d0..38999c0 100644 --- a/exercises/13_hash_tables/hash3.lisp +++ b/exercises/13_hash_tables/hash3.lisp @@ -3,6 +3,7 @@ ;;; The classic hash-table job: tally. GETHASH takes a default: ;;; (gethash x table 0) ;;; so a missing key counts as 0 and INCF can do the rest. +;;; (hash-table-count table) is the number of stored keys. ;; TODO: Count the occurrences of each element of XS. Return the ;;; table. diff --git a/info.sexp b/info.sexp index 85f2010..9c0083f 100644 --- a/info.sexp +++ b/info.sexp @@ -72,7 +72,7 @@ Found an issue, or want to add an exercise? Improvements welcome!") :hint "The condition needs to be a TEST, not the temperature itself: (> temperature 30). Any number - even 0 - is true, so the broken version always answers hot.") (:name "if2" :dir "04_conditionals" - :hint "IF takes exactly one then-form and one else-form; the fourth part makes it malformed. COND handles many branches: (cond ((zerop n) \"zero\") ((minusp n) \"negative\") (t \"positive\")).") + :hint "IF takes exactly one then-form and at most one else-form; the fourth part makes it malformed. COND handles many branches: (cond ((zerop n) \"zero\") ((minusp n) \"negative\") (t \"positive\")).") (:name "if3" :dir "04_conditionals" :hint "WHEN runs its body when the test is TRUE - plusp, not minusp. For enter, only an IF with both branches can return :denied when the door is shut.") @@ -141,7 +141,7 @@ Found an issue, or want to add an exercise? Improvements welcome!") :hint "FORMAT iterates with ~{ ... ~}, and ~^ skips the separator at the last element: (format nil \"~{~a~^, ~}\" xs).") (:name "loops1" :dir "10_loops" - :hint "DOLIST walks a list: (dolist (x xs) (incf total x)). DOTIMES counts from 0; PUSH inside it builds the countdown naturally.") + :hint "DOLIST walks a list: (dolist (x xs) (setf total (+ total x))). DOTIMES counts from 0; PUSH inside it builds the countdown naturally.") (:name "loops2" :dir "10_loops" :hint "COLLECT gathers into a list, SUM adds: (loop for i from 1 to n sum i).") diff --git a/solutions/04_conditionals/if1.lisp b/solutions/04_conditionals/if1.lisp index fb70682..9e24a0a 100644 --- a/solutions/04_conditionals/if1.lisp +++ b/solutions/04_conditionals/if1.lisp @@ -13,3 +13,4 @@ (check-equal "hot" (classify 35)) (check-equal "pleasant" (classify 20)) (check-equal "hot" (classify 31)) +(check-equal "pleasant" (classify 30)) diff --git a/solutions/04_conditionals/if3.lisp b/solutions/04_conditionals/if3.lisp index 4fb93f8..ece9947 100644 --- a/solutions/04_conditionals/if3.lisp +++ b/solutions/04_conditionals/if3.lisp @@ -14,5 +14,6 @@ (check-equal nil (only-positive-squares -3)) (check-equal 9 (only-positive-squares 3)) +(check-equal nil (only-positive-squares 0)) (check-equal :welcome (enter t)) (check-equal :denied (enter nil)) diff --git a/solutions/05_predicates/pred1.lisp b/solutions/05_predicates/pred1.lisp index a7c2899..948ab72 100644 --- a/solutions/05_predicates/pred1.lisp +++ b/solutions/05_predicates/pred1.lisp @@ -12,3 +12,5 @@ (check (truthy-p "")) (check (not (truthy-p nil))) (check (not (truthy-p '()))) +(check-equal t (truthy-p 0)) +(check-equal nil (truthy-p nil)) diff --git a/solutions/05_predicates/pred3.lisp b/solutions/05_predicates/pred3.lisp index 0dd7969..bd93333 100644 --- a/solutions/05_predicates/pred3.lisp +++ b/solutions/05_predicates/pred3.lisp @@ -16,3 +16,4 @@ (check-equal :string (describe-value "hi")) (check-equal :list (describe-value '(a b))) (check-equal :nothing (describe-value nil)) +(check-equal :other (describe-value 'hi)) diff --git a/solutions/06_lists/lists5.lisp b/solutions/06_lists/lists5.lisp index 97d72f9..6d120f9 100644 --- a/solutions/06_lists/lists5.lisp +++ b/solutions/06_lists/lists5.lisp @@ -19,5 +19,5 @@ (check-equal 2 (price 'tea)) (check-equal nil (price 'chai)) (check (let ((menu (add-item 'chai 5))) - (and (eql 5 (price 'chai)) - (eql 3 (price 'coffee))))) + (and (eql 5 (cdr (assoc 'chai menu))) + (eql 3 (cdr (assoc 'coffee menu)))))) diff --git a/solutions/08_functional/func4.lisp b/solutions/08_functional/func4.lisp index 9ae0433..2f4d4f3 100644 --- a/solutions/08_functional/func4.lisp +++ b/solutions/08_functional/func4.lisp @@ -13,6 +13,7 @@ (check-equal 4 (biggest '(3 1 4))) (check-equal 2 (biggest '(2))) +(check-equal 9 (biggest '(1 9 2))) (check-equal 6 (product '(1 2 3))) (check-equal 7 (product '(7))) (check-equal 1 (product '())) diff --git a/solutions/10_loops/loops1.lisp b/solutions/10_loops/loops1.lisp index f94afdb..97aa639 100644 --- a/solutions/10_loops/loops1.lisp +++ b/solutions/10_loops/loops1.lisp @@ -3,7 +3,7 @@ (defun sum-of (xs) (let ((total 0)) (dolist (x xs) - (incf total x)) + (setf total (+ total x))) total)) (defun countdown (n) diff --git a/solutions/11_numbers/num2.lisp b/solutions/11_numbers/num2.lisp index 60c85e6..b354af2 100644 --- a/solutions/11_numbers/num2.lisp +++ b/solutions/11_numbers/num2.lisp @@ -14,6 +14,7 @@ (check-equal 10 (floor-to-five 12)) (check-equal 10 (floor-to-five 14)) (check-equal 15 (floor-to-five 15)) +(check-equal -15 (floor-to-five -12)) (check-equal 4 (mod-five 9)) (check-equal 0 (mod-five -5)) (check-equal 1 (mod-five -9)) diff --git a/solutions/12_structs/structs3.lisp b/solutions/12_structs/structs3.lisp index c63412d..f091325 100644 --- a/solutions/12_structs/structs3.lisp +++ b/solutions/12_structs/structs3.lisp @@ -16,9 +16,9 @@ ;;; them unchanged. ;;; ------------------------------------------------------------------ -(check (let ((c (make-counter))) +(check (let ((c (make-counter :value 9))) (bump c) - (eql 1 (counter-value c)))) + (eql 10 (counter-value c)))) (check (let ((c (make-counter :value 9))) (reset-counter c) (eql 0 (counter-value c))))