From 2735c8dca59ec792a59c562d23635b1f021756df Mon Sep 17 00:00:00 2001 From: venomade Date: Sun, 22 Nov 2026 04:31:29 +0000 Subject: [PATCH] Fix 12 exercise defects from review wave 1 (scouts a+d) --- exercises/00_intro/README.md | 8 ++++++++ exercises/01_sexps/sexps1.lisp | 3 +++ exercises/01_sexps/sexps2.lisp | 3 +++ exercises/01_sexps/sexps3.lisp | 3 +++ exercises/02_functions/functions3.lisp | 2 ++ exercises/02_functions/functions4.lisp | 5 +++-- exercises/03_variables/variables2.lisp | 4 ++-- exercises/03_variables/variables4.lisp | 1 + exercises/14_packages/pkg2.lisp | 1 + exercises/16_conditions/cond3.lisp | 1 + exercises/16_conditions/cond4.lisp | 5 +++++ exercises/18_dynamic/dyn2.lisp | 3 +++ solutions/02_functions/functions3.lisp | 1 + solutions/02_functions/functions4.lisp | 3 +++ solutions/16_conditions/cond3.lisp | 1 + solutions/16_conditions/cond4.lisp | 5 +++++ solutions/18_dynamic/dyn2.lisp | 3 +++ solutions/quizzes/quiz2.lisp | 2 +- 18 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 exercises/00_intro/README.md diff --git a/exercises/00_intro/README.md b/exercises/00_intro/README.md new file mode 100644 index 0000000..a7b5e0a --- /dev/null +++ b/exercises/00_intro/README.md @@ -0,0 +1,8 @@ +# 00_intro + +The Lisplings flow, and reading your first error. + +intro1 is error-free: edit it, save it, and watch the exercise run +again in the terminal (press n to move on, h for a hint). intro2 +does not load; read the error in the terminal and fix it so the +greeting prints. diff --git a/exercises/01_sexps/sexps1.lisp b/exercises/01_sexps/sexps1.lisp index fe91043..687918b 100644 --- a/exercises/01_sexps/sexps1.lisp +++ b/exercises/01_sexps/sexps1.lisp @@ -3,6 +3,9 @@ ;;; In Lisp the operator comes first: (+ 1 2) is 3. Nesting needs no ;;; precedence rules at all: (* 2 (+ 3 4)) is 14. ;;; +;;; (defun name () ...) just names the expression; chapter 02 +;;; explains it. +;;; ;;; Fix each definition. ;; TODO: Add 10 and 32. diff --git a/exercises/01_sexps/sexps2.lisp b/exercises/01_sexps/sexps2.lisp index f30b9ae..cf2aa65 100644 --- a/exercises/01_sexps/sexps2.lisp +++ b/exercises/01_sexps/sexps2.lisp @@ -4,6 +4,9 @@ ;;; ;;; A Lisp delight: division is exact. (/ 1 2) is the rational 1/2, ;;; not 0.5. Integers, rationals and floats form one number tower. +;;; +;;; (defun name () ...) just names the expression; chapter 02 +;;; explains it. ;; TODO: (2 + 3) multiplied by (4 - 1). Should be 15. (defun calc-a () diff --git a/exercises/01_sexps/sexps3.lisp b/exercises/01_sexps/sexps3.lisp index 06549d9..c5ee92e 100644 --- a/exercises/01_sexps/sexps3.lisp +++ b/exercises/01_sexps/sexps3.lisp @@ -7,6 +7,9 @@ ;;; '(+ 1 2) evaluates to the list (+ 1 2) ;;; ;;; 'x is reader shorthand for (quote x). +;;; +;;; (defun name () ...) just names the expression; chapter 02 +;;; explains it. ;; TODO: The form itself, not its value. (defun the-form () diff --git a/exercises/02_functions/functions3.lisp b/exercises/02_functions/functions3.lisp index 2e71a54..d50418c 100644 --- a/exercises/02_functions/functions3.lisp +++ b/exercises/02_functions/functions3.lisp @@ -6,6 +6,7 @@ ;;; earlier parameter. ;; TODO: Repeat WORD N times. N is optional and defaults to 2. +;; make-list builds a list of n copies (lists arrive in ch. 06). (defun repeat-word (word n) (make-list n :initial-element word)) @@ -22,5 +23,6 @@ (check-equal '("hi" "hi") (repeat-word "hi")) (check-equal '("hi" "hi" "hi" "hi") (repeat-word "hi" 4)) (check-equal '(:width 2 :height 2 :depth 2) (make-box 2)) +(check-equal '(:width 3 :height 3 :depth 3) (make-box 3)) (check-equal '(:width 2 :height 5 :depth 2) (make-box 2 :height 5)) (check-equal '(:width 2 :height 5 :depth 7) (make-box 2 :height 5 :depth 7)) diff --git a/exercises/02_functions/functions4.lisp b/exercises/02_functions/functions4.lisp index a026793..0442475 100644 --- a/exercises/02_functions/functions4.lisp +++ b/exercises/02_functions/functions4.lisp @@ -3,8 +3,9 @@ ;;; A function may return several values at once: ;;; (values 1 2) => 1 and 2 ;;; Everything except the first value rides along, waiting for -;;; MULTIPLE-VALUE-BIND or NTH-VALUE to pick it up. FLOOR is a -;;; classic: (floor 10 3) returns 3 AND 1. +;;; MULTIPLE-VALUE-BIND or NTH-VALUE to pick it up. The checks +;;; below gather the values with MULTIPLE-VALUE-LIST, turning them +;;; into a list. FLOOR is a classic: (floor 10 3) returns 3 AND 1. ;; TODO: Return TWO values: the whole part of A/B and the rest. (defun divide (a b) diff --git a/exercises/03_variables/variables2.lisp b/exercises/03_variables/variables2.lisp index 3611a0c..216103f 100644 --- a/exercises/03_variables/variables2.lisp +++ b/exercises/03_variables/variables2.lisp @@ -4,8 +4,8 @@ ;;; LET may reuse a name: the new binding shadows the old one, and ;;; the initializer still evaluates in the outer scope. -;; TODO: The inner X should be the outer X plus 5 - make it so, -;;; without changing either number. +;; TODO: Keep the outer 15; make the inner binding the outer X +;; plus 5. (defun shadow-add () (let ((x 15)) (let ((x 15)) diff --git a/exercises/03_variables/variables4.lisp b/exercises/03_variables/variables4.lisp index 9a73771..5fea806 100644 --- a/exercises/03_variables/variables4.lisp +++ b/exercises/03_variables/variables4.lisp @@ -2,6 +2,7 @@ ;;; ;;; SETF assigns to any "place": a variable, a list element, a ;;; property. The general shape is (setf PLACE new-value). +;;; (getf *account* :balance) reads the property back. (defparameter *account* (list :owner "Ada" :balance 100)) diff --git a/exercises/14_packages/pkg2.lisp b/exercises/14_packages/pkg2.lisp index aa18314..f6a4d9d 100644 --- a/exercises/14_packages/pkg2.lisp +++ b/exercises/14_packages/pkg2.lisp @@ -3,6 +3,7 @@ ;;; A single colon reaches only EXPORTED symbols. The kitchen ;;; makes soup; only the soup is on the menu. +;; TODO: The defpackage below is missing (:export #:make-soup). (defpackage #:kitchen (:use #:common-lisp)) diff --git a/exercises/16_conditions/cond3.lisp b/exercises/16_conditions/cond3.lisp index 2d88008..be584f0 100644 --- a/exercises/16_conditions/cond3.lisp +++ b/exercises/16_conditions/cond3.lisp @@ -24,3 +24,4 @@ (check-equal "yum" (catch-empty '(a b))) (check-equal "soup" (catch-empty '())) +(check-error (eat '()) 'empty-plate) diff --git a/exercises/16_conditions/cond4.lisp b/exercises/16_conditions/cond4.lisp index b56ee1d..cdf4049 100644 --- a/exercises/16_conditions/cond4.lisp +++ b/exercises/16_conditions/cond4.lisp @@ -31,3 +31,8 @@ (check-equal 1 (car-with-default '(1 2 3))) (check-equal nil (car-with-default "not a list")) (check-equal nil (car-with-default nil)) +(check-equal nil + (handler-bind ((error (lambda (e) + (declare (ignore e)) + (invoke-restart (find-restart 'use-nil))))) + (safe-car "not a list"))) diff --git a/exercises/18_dynamic/dyn2.lisp b/exercises/18_dynamic/dyn2.lisp index 23efd94..a7f46b7 100644 --- a/exercises/18_dynamic/dyn2.lisp +++ b/exercises/18_dynamic/dyn2.lisp @@ -24,3 +24,6 @@ (check (eql 0 (current-depth))) (check (eql 3 (let ((*depth* 3)) (current-depth)))) (check (eql 0 (current-depth))) +(check (eql 42 (let ((*depth* 42)) + (with-depth 7 (lambda () nil)) + (current-depth)))) diff --git a/solutions/02_functions/functions3.lisp b/solutions/02_functions/functions3.lisp index a2019f6..bdc037a 100644 --- a/solutions/02_functions/functions3.lisp +++ b/solutions/02_functions/functions3.lisp @@ -14,5 +14,6 @@ (check-equal '("hi" "hi") (repeat-word "hi")) (check-equal '("hi" "hi" "hi" "hi") (repeat-word "hi" 4)) (check-equal '(:width 2 :height 2 :depth 2) (make-box 2)) +(check-equal '(:width 3 :height 3 :depth 3) (make-box 3)) (check-equal '(:width 2 :height 5 :depth 2) (make-box 2 :height 5)) (check-equal '(:width 2 :height 5 :depth 7) (make-box 2 :height 5 :depth 7)) diff --git a/solutions/02_functions/functions4.lisp b/solutions/02_functions/functions4.lisp index 5a22051..6343de7 100644 --- a/solutions/02_functions/functions4.lisp +++ b/solutions/02_functions/functions4.lisp @@ -1,4 +1,7 @@ ;;; FUNCTIONS 4: MULTIPLE VALUES (solution) +;;; +;;; The checks below gather the values with MULTIPLE-VALUE-LIST, +;;; turning them into a list. (defun divide (a b) (floor a b)) diff --git a/solutions/16_conditions/cond3.lisp b/solutions/16_conditions/cond3.lisp index 2a6b740..6fb301e 100644 --- a/solutions/16_conditions/cond3.lisp +++ b/solutions/16_conditions/cond3.lisp @@ -20,3 +20,4 @@ (check-equal "yum" (catch-empty '(a b))) (check-equal "soup" (catch-empty '())) +(check-error (eat '()) 'empty-plate) diff --git a/solutions/16_conditions/cond4.lisp b/solutions/16_conditions/cond4.lisp index 2f38b8d..41c8e7e 100644 --- a/solutions/16_conditions/cond4.lisp +++ b/solutions/16_conditions/cond4.lisp @@ -21,3 +21,8 @@ (check-equal 1 (car-with-default '(1 2 3))) (check-equal nil (car-with-default "not a list")) (check-equal nil (car-with-default nil)) +(check-equal nil + (handler-bind ((error (lambda (e) + (declare (ignore e)) + (invoke-restart (find-restart 'use-nil))))) + (safe-car "not a list"))) diff --git a/solutions/18_dynamic/dyn2.lisp b/solutions/18_dynamic/dyn2.lisp index 95d2878..cd1a61f 100644 --- a/solutions/18_dynamic/dyn2.lisp +++ b/solutions/18_dynamic/dyn2.lisp @@ -18,3 +18,6 @@ (check (eql 0 (current-depth))) (check (eql 3 (let ((*depth* 3)) (current-depth)))) (check (eql 0 (current-depth))) +(check (eql 42 (let ((*depth* 42)) + (with-depth 7 (lambda () nil)) + (current-depth)))) diff --git a/solutions/quizzes/quiz2.lisp b/solutions/quizzes/quiz2.lisp index 9aa5708..1d5cb01 100644 --- a/solutions/quizzes/quiz2.lisp +++ b/solutions/quizzes/quiz2.lisp @@ -3,7 +3,7 @@ (defun eval-expr (expr) (cond ((numberp expr) expr) ((and (consp expr) (eq 'quote (car expr))) - (second expr)) + (car (cdr expr))) ((and (consp expr) (member (car expr) '(+ - *))) (apply (car expr) (mapcar #'eval-expr (cdr expr)))) (t (error "cannot evaluate ~s" expr))))