Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table

This commit is contained in:
venomade committed 2026-11-22 11:15:35 +00:00
1 parent 77843402fa
commit 4675bd1106
76 files changed
+2505 -14

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
;;; LOOPS 4: LOOP'S OTHER CLAUSES (solution)
(defun doublings (n)
(loop repeat n
for x = 1 then (* x 2)
collect x))
(defun cart-until-cap (xs cap)
(loop with total = 0
for p in xs
until (>= (+ total p) cap)
do (setf total (+ total p))
collect p))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(1 2 4 8) (doublings 4))
(check-equal '(1) (doublings 1))
(check-equal '() (doublings 0))
(check-equal '(3 5) (cart-until-cap '(3 5 2 8) 10))
(check-equal '() (cart-until-cap '(20 1) 10))
(check-equal '(1 2) (cart-until-cap '(1 2 3 4) 6))
+24
View File
@@ -0,0 +1,24 @@
;;; LOOPS 5: DO AND DO* (solution)
(defun triangle-do (n)
(do ((i 1 (1+ i))
(total 0 (+ total i)))
((> i n) total)))
(defun factorial-of (n)
(do* ((a 1 (1+ a))
(b 1 (* b a)))
((>= a n) b)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 6 (triangle-do 3))
(check-equal 55 (triangle-do 10))
(check-equal 0 (triangle-do 0))
(check-equal 1 (factorial-of 0))
(check-equal 1 (factorial-of 1))
(check-equal 6 (factorial-of 3))
(check-equal 24 (factorial-of 4))
+35
View File
@@ -0,0 +1,35 @@
;;; LOOPS 6: COUNT, MAXIMIZE, LOOP-FINISH (solution)
(defun count-odds (xs)
(loop for x in xs
count (oddp x)))
(defun widest (xs)
(if (null xs)
nil
(loop for x in xs
maximize x)))
(defun first-sum-over (xs limit)
(loop with total = 0
for x in xs
do (if (> (+ total x) limit)
(loop-finish)
(setf total (+ total x)))
finally (return total)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 3 (count-odds '(1 2 3 4 5)))
(check-equal 0 (count-odds '(2 4)))
(check-equal 0 (count-odds '()))
(check-equal 9 (widest '(3 9 2)))
(check-equal 4 (widest '(4)))
(check-equal nil (widest '()))
(check-equal 5 (first-sum-over '(2 3 4) 8))
(check-equal 9 (first-sum-over '(2 3 4) 100))
(check-equal 0 (first-sum-over '(9 9) 8))
(check-equal 0 (first-sum-over '() 5))