Fix 16 mid-chapter exercise defects (scouts mid1+mid2)

Verified: dev/verify.sh all 70 exercises green (broken fail, solved pass).
This commit is contained in:
venomade committed 2026-11-22 04:52:35 +00:00
1 parent 2735c8dca5
commit 6e2c382c78
24 files changed
+34 -15

No files matched your search

+1
View File
@@ -20,3 +20,4 @@
(check-equal "hot" (classify 35)) (check-equal "hot" (classify 35))
(check-equal "pleasant" (classify 20)) (check-equal "pleasant" (classify 20))
(check-equal "hot" (classify 31)) (check-equal "hot" (classify 31))
(check-equal "pleasant" (classify 30))
+1
View File
@@ -21,5 +21,6 @@
(check-equal nil (only-positive-squares -3)) (check-equal nil (only-positive-squares -3))
(check-equal 9 (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 :welcome (enter t))
(check-equal :denied (enter nil)) (check-equal :denied (enter nil))
+2
View File
@@ -16,3 +16,5 @@
(check (truthy-p "")) (check (truthy-p ""))
(check (not (truthy-p nil))) (check (not (truthy-p nil)))
(check (not (truthy-p '()))) (check (not (truthy-p '())))
(check-equal t (truthy-p 0))
(check-equal nil (truthy-p nil))
+1
View File
@@ -22,3 +22,4 @@
(check-equal :string (describe-value "hi")) (check-equal :string (describe-value "hi"))
(check-equal :list (describe-value '(a b))) (check-equal :list (describe-value '(a b)))
(check-equal :nothing (describe-value nil)) (check-equal :nothing (describe-value nil))
(check-equal :other (describe-value 'hi))
+2 -2
View File
@@ -25,5 +25,5 @@
(check-equal 2 (price 'tea)) (check-equal 2 (price 'tea))
(check-equal nil (price 'chai)) (check-equal nil (price 'chai))
(check (let ((menu (add-item 'chai 5))) (check (let ((menu (add-item 'chai 5)))
(and (eql 5 (price 'chai)) (and (eql 5 (cdr (assoc 'chai menu)))
(eql 3 (price 'coffee))))) (eql 3 (cdr (assoc 'coffee menu))))))
+1
View File
@@ -1,5 +1,6 @@
;;; RECURSION 4: WALKING A TREE ;;; 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 ;;; Lists nest, and a tree has TWO recursive parts: the first
;;; element and the rest. Recurse into both. ;;; element and the rest. Recurse into both.
+2
View File
@@ -3,6 +3,8 @@
;;; MAPCAR applies a function to each element and collects the ;;; MAPCAR applies a function to each element and collects the
;;; results into a new list: ;;; results into a new list:
;;; (mapcar #'1+ '(1 2 3)) => (2 3 4) ;;; (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. ;; TODO: Every element of XS, doubled.
(defun double-all (xs) (defun double-all (xs)
+1
View File
@@ -3,6 +3,7 @@
;;; FUNCALL spells the arguments out; APPLY takes the last of them ;;; FUNCALL spells the arguments out; APPLY takes the last of them
;;; from a list: ;;; from a list:
;;; (apply #'+ 1 2 '(3 4)) => 10 ;;; (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. ;; TODO: Add up all of XS, using APPLY.
(defun sum-of (xs) (defun sum-of (xs)
+1
View File
@@ -19,6 +19,7 @@
(check-equal 4 (biggest '(3 1 4))) (check-equal 4 (biggest '(3 1 4)))
(check-equal 2 (biggest '(2))) (check-equal 2 (biggest '(2)))
(check-equal 9 (biggest '(1 9 2)))
(check-equal 6 (product '(1 2 3))) (check-equal 6 (product '(1 2 3)))
(check-equal 7 (product '(7))) (check-equal 7 (product '(7)))
(check-equal 1 (product '())) (check-equal 1 (product '()))
+3 -3
View File
@@ -1,8 +1,8 @@
;;; STRINGS 1: FORMAT DIRECTIVES ;;; STRINGS 1: FORMAT DIRECTIVES
;;; ;;;
;;; FORMAT is a little language: ~a prints readably, ~10a pads to a ;;; FORMAT is a little language: ~a prints a value plainly; ~s
;;; width, ~% is a newline. The first argument NIL means "return a ;;; prints it readably. ~10a pads to a width, ~% is a newline. The
;;; string" instead of printing. ;;; first argument NIL means "return a string" instead of printing.
;;; (format nil "Hello, ~a!" "Ada") => "Hello, Ada!" ;;; (format nil "Hello, ~a!" "Ada") => "Hello, Ada!"
;; TODO: "Ada has 42 cats." with the name and count in place. ;; TODO: "Ada has 42 cats." with the name and count in place.
+1
View File
@@ -20,6 +20,7 @@
(check-equal 10 (floor-to-five 12)) (check-equal 10 (floor-to-five 12))
(check-equal 10 (floor-to-five 14)) (check-equal 10 (floor-to-five 14))
(check-equal 15 (floor-to-five 15)) (check-equal 15 (floor-to-five 15))
(check-equal -15 (floor-to-five -12))
(check-equal 4 (mod-five 9)) (check-equal 4 (mod-five 9))
(check-equal 0 (mod-five -5)) (check-equal 0 (mod-five -5))
(check-equal 1 (mod-five -9)) (check-equal 1 (mod-five -9))
+1 -1
View File
@@ -2,7 +2,7 @@
;;; ;;;
;;; DEFSTRUCT defines a record type and generates, for free: ;;; DEFSTRUCT defines a record type and generates, for free:
;;; a constructor (make-person), slot readers (person-name), ;;; a constructor (make-person), slot readers (person-name),
;;; and a predicate (personp). ;;; and a predicate (person-p).
(defstruct person (defstruct person
name name
+2 -2
View File
@@ -19,9 +19,9 @@
;;; them unchanged. ;;; them unchanged.
;;; ------------------------------------------------------------------ ;;; ------------------------------------------------------------------
(check (let ((c (make-counter))) (check (let ((c (make-counter :value 9)))
(bump c) (bump c)
(eql 1 (counter-value c)))) (eql 10 (counter-value c))))
(check (let ((c (make-counter :value 9))) (check (let ((c (make-counter :value 9)))
(reset-counter c) (reset-counter c)
(eql 0 (counter-value c)))) (eql 0 (counter-value c))))
+1
View File
@@ -3,6 +3,7 @@
;;; The classic hash-table job: tally. GETHASH takes a default: ;;; The classic hash-table job: tally. GETHASH takes a default:
;;; (gethash x table 0) ;;; (gethash x table 0)
;;; so a missing key counts as 0 and INCF can do the rest. ;;; 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 ;; TODO: Count the occurrences of each element of XS. Return the
;;; table. ;;; table.
+2 -2
View File
@@ -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.") :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" (:name "if2"
:dir "04_conditionals" :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" (:name "if3"
:dir "04_conditionals" :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.") :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).") :hint "FORMAT iterates with ~{ ... ~}, and ~^ skips the separator at the last element: (format nil \"~{~a~^, ~}\" xs).")
(:name "loops1" (:name "loops1"
:dir "10_loops" :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" (:name "loops2"
:dir "10_loops" :dir "10_loops"
:hint "COLLECT gathers into a list, SUM adds: (loop for i from 1 to n sum i).") :hint "COLLECT gathers into a list, SUM adds: (loop for i from 1 to n sum i).")
+1
View File
@@ -13,3 +13,4 @@
(check-equal "hot" (classify 35)) (check-equal "hot" (classify 35))
(check-equal "pleasant" (classify 20)) (check-equal "pleasant" (classify 20))
(check-equal "hot" (classify 31)) (check-equal "hot" (classify 31))
(check-equal "pleasant" (classify 30))
+1
View File
@@ -14,5 +14,6 @@
(check-equal nil (only-positive-squares -3)) (check-equal nil (only-positive-squares -3))
(check-equal 9 (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 :welcome (enter t))
(check-equal :denied (enter nil)) (check-equal :denied (enter nil))
+2
View File
@@ -12,3 +12,5 @@
(check (truthy-p "")) (check (truthy-p ""))
(check (not (truthy-p nil))) (check (not (truthy-p nil)))
(check (not (truthy-p '()))) (check (not (truthy-p '())))
(check-equal t (truthy-p 0))
(check-equal nil (truthy-p nil))
+1
View File
@@ -16,3 +16,4 @@
(check-equal :string (describe-value "hi")) (check-equal :string (describe-value "hi"))
(check-equal :list (describe-value '(a b))) (check-equal :list (describe-value '(a b)))
(check-equal :nothing (describe-value nil)) (check-equal :nothing (describe-value nil))
(check-equal :other (describe-value 'hi))
+2 -2
View File
@@ -19,5 +19,5 @@
(check-equal 2 (price 'tea)) (check-equal 2 (price 'tea))
(check-equal nil (price 'chai)) (check-equal nil (price 'chai))
(check (let ((menu (add-item 'chai 5))) (check (let ((menu (add-item 'chai 5)))
(and (eql 5 (price 'chai)) (and (eql 5 (cdr (assoc 'chai menu)))
(eql 3 (price 'coffee))))) (eql 3 (cdr (assoc 'coffee menu))))))
+1
View File
@@ -13,6 +13,7 @@
(check-equal 4 (biggest '(3 1 4))) (check-equal 4 (biggest '(3 1 4)))
(check-equal 2 (biggest '(2))) (check-equal 2 (biggest '(2)))
(check-equal 9 (biggest '(1 9 2)))
(check-equal 6 (product '(1 2 3))) (check-equal 6 (product '(1 2 3)))
(check-equal 7 (product '(7))) (check-equal 7 (product '(7)))
(check-equal 1 (product '())) (check-equal 1 (product '()))
+1 -1
View File
@@ -3,7 +3,7 @@
(defun sum-of (xs) (defun sum-of (xs)
(let ((total 0)) (let ((total 0))
(dolist (x xs) (dolist (x xs)
(incf total x)) (setf total (+ total x)))
total)) total))
(defun countdown (n) (defun countdown (n)
+1
View File
@@ -14,6 +14,7 @@
(check-equal 10 (floor-to-five 12)) (check-equal 10 (floor-to-five 12))
(check-equal 10 (floor-to-five 14)) (check-equal 10 (floor-to-five 14))
(check-equal 15 (floor-to-five 15)) (check-equal 15 (floor-to-five 15))
(check-equal -15 (floor-to-five -12))
(check-equal 4 (mod-five 9)) (check-equal 4 (mod-five 9))
(check-equal 0 (mod-five -5)) (check-equal 0 (mod-five -5))
(check-equal 1 (mod-five -9)) (check-equal 1 (mod-five -9))
+2 -2
View File
@@ -16,9 +16,9 @@
;;; them unchanged. ;;; them unchanged.
;;; ------------------------------------------------------------------ ;;; ------------------------------------------------------------------
(check (let ((c (make-counter))) (check (let ((c (make-counter :value 9)))
(bump c) (bump c)
(eql 1 (counter-value c)))) (eql 10 (counter-value c))))
(check (let ((c (make-counter :value 9))) (check (let ((c (make-counter :value 9)))
(reset-counter c) (reset-counter c)
(eql 0 (counter-value c)))) (eql 0 (counter-value c))))