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

+29
View File
@@ -0,0 +1,29 @@
;;; VECTORS 1: MAKE-ARRAY, AREF, AND #(...) (solution)
(defun slot-one (v)
(aref v 1))
(defun replace-first (v x)
(setf (aref v 0) x)
v)
(defun triple-vector (a b c)
(vector a b c))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 'b (slot-one #(a b c)))
(check-equal 2 (slot-one #(1 2 3)))
(check-equal 'x
(aref (replace-first (make-array 2 :initial-element 'a) 'x)
0))
(check-equal 99
(aref (replace-first (make-array 3 :initial-element 0) 99)
0))
(check (vectorp (triple-vector 'a 'b 'c)))
(check (equalp #(p q r) (triple-vector 'p 'q 'r)))
(check-equal 'q (aref (triple-vector 'p 'q 'r) 1))
(check-equal 3 (length #(a b c)))
+39
View File
@@ -0,0 +1,39 @@
;;; VECTORS 2: FILL POINTERS AND GROWING VECTORS (solution)
(defun blank-row (room)
(make-array room :fill-pointer 0))
(defun triple-push (v x)
(vector-push x v)
(vector-push x v)
(vector-push x v)
v)
(defun growable (a b)
(make-array 2 :initial-contents (list a b) :fill-pointer 2
:adjustable t))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 0 (fill-pointer (blank-row 4)))
(check-equal 0 (vector-push 'z (blank-row 5)))
(check (let ((v (blank-row 3)))
(vector-push 'a v)
(equal 1 (fill-pointer v))))
(check-equal nil (vector-push 'x (make-array 2 :fill-pointer 2)))
(check-error (fill-pointer (make-array 3)))
(check-equal 3
(fill-pointer (triple-push (make-array 3 :fill-pointer 0)
'q)))
(check-equal 'q
(aref (triple-push (make-array 3 :fill-pointer 0) 'q)
2))
(check (let ((v (growable 'a 'b)))
(vector-push-extend 'c v)
(equal 'c (aref v 2))))
(check (let ((v (growable 1 2)))
(vector-push-extend 9 v)
(equal 9 (aref v 2))))
+29
View File
@@ -0,0 +1,29 @@
;;; VECTORS 3: STRINGS ARE VECTORS (solution)
(defun first-letter (w)
(aref w 0))
(defun retag (w c)
(let ((s (make-array (length w) :element-type 'character
:initial-contents w)))
(setf (aref s (- (length s) 1)) c)
s))
(defun letter-run (n c)
(make-array n :element-type 'character :initial-element c))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (vectorp "cat"))
(check-equal #\c (first-letter "cat"))
(check-equal #\a (first-letter "apple"))
(check-equal "cab" (retag "cat" #\b))
(check-equal "suns" (retag "sung" #\s))
(check (let ((w "cat"))
(retag w #\b)
(equal "cat" w)))
(check-equal "aaa" (letter-run 3 #\a))
(check-equal "xx" (letter-run 2 #\x))