Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table
This commit is contained in:
1 parent
77843402fa
commit
4675bd1106
76 files changed
+2505
-14
No files matched your search
@@ -0,0 +1,7 @@
|
||||
# 20_vectors
|
||||
|
||||
MAKE-ARRAY, AREF, fill pointers, and strings as vectors.
|
||||
|
||||
A vector is an array with a single index. MAKE-ARRAY builds one,
|
||||
AREF reads and writes its slots, and a fill pointer marks how far
|
||||
it is filled. A string is a vector of characters.
|
||||
@@ -0,0 +1,42 @@
|
||||
;;; VECTORS 1: MAKE-ARRAY, AREF, AND #(...)
|
||||
;;;
|
||||
;;; A vector is a one-dimensional array: a row of slots numbered
|
||||
;;; from 0. Lists and vectors are both sequences.
|
||||
;;; (make-array 3) => #(NIL NIL NIL)
|
||||
;;; (make-array 3 :initial-element 0) => #(0 0 0)
|
||||
;;; (vector 1 2 3) => #(1 2 3)
|
||||
;;; #(1 2 3) is that same vector, written literally
|
||||
;;; (aref #(a b c) 1) => B
|
||||
;;; (setf (aref v 0) 'x) replaces slot 0
|
||||
;;; (vectorp #(1 2)) => T, (vectorp '(1 2)) => NIL
|
||||
;;; (length #(1 2 3)) => 3
|
||||
|
||||
;; TODO: The element stored in slot 1 of V.
|
||||
(defun slot-one (v)
|
||||
(aref v 0))
|
||||
|
||||
;; TODO: Put X in slot 0 of V and return V.
|
||||
(defun replace-first (v x)
|
||||
v)
|
||||
|
||||
;; TODO: A fresh vector with A, B, and C in its three slots.
|
||||
(defun triple-vector (a b c)
|
||||
(list 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)))
|
||||
@@ -0,0 +1,48 @@
|
||||
;;; VECTORS 2: FILL POINTERS AND GROWING VECTORS
|
||||
;;;
|
||||
;;; A vector can carry a fill pointer: the number of slots in use.
|
||||
;;; VECTOR-PUSH stores a value past it and returns the slot index;
|
||||
;;; on a full vector it returns NIL instead.
|
||||
;;; (make-array 4 :fill-pointer 0) => empty, with room for 4
|
||||
;;; (fill-pointer v) => slots in use
|
||||
;;; (vector-push 'x v) => 0, the index it landed on
|
||||
;;; VECTOR-PUSH-EXTEND grows the vector itself when it fills up, so
|
||||
;;; the vector must be made :adjustable:
|
||||
;;; (make-array 2 :fill-pointer 2 :adjustable t)
|
||||
|
||||
;; TODO: A vector with room for ROOM items, none in use yet.
|
||||
(defun blank-row (room)
|
||||
(make-array room))
|
||||
|
||||
;; TODO: Push X onto V three times, then return V.
|
||||
(defun triple-push (v x)
|
||||
v)
|
||||
|
||||
;; TODO: A fresh growable vector holding A and B.
|
||||
(defun growable (a b)
|
||||
(make-array 2 :initial-contents (list a b) :fill-pointer 2))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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))))
|
||||
@@ -0,0 +1,41 @@
|
||||
;;; VECTORS 3: STRINGS ARE VECTORS
|
||||
;;;
|
||||
;;; A string is a vector of characters, so the vector tools work on
|
||||
;;; it unchanged. #\a is the character a.
|
||||
;;; (aref "cat" 0) => #\c
|
||||
;;; (length "cat") => 3
|
||||
;;; (vectorp "cat") => T
|
||||
;;; MAKE-ARRAY with :element-type 'character builds strings:
|
||||
;;; (make-array 3 :element-type 'character :initial-element #\a)
|
||||
;;; => "aaa"
|
||||
;;; To change a string, work on your own copy: MAKE-ARRAY with
|
||||
;;; :initial-contents duplicates an existing one.
|
||||
|
||||
;; TODO: The first character of the word W.
|
||||
(defun first-letter (w)
|
||||
(aref w 1))
|
||||
|
||||
;; TODO: W with its last character replaced by C. Leave the caller's
|
||||
;;; string alone: build a copy first.
|
||||
(defun retag (w c)
|
||||
w)
|
||||
|
||||
;; TODO: A fresh string of N copies of the character C.
|
||||
(defun letter-run (n c)
|
||||
(make-array n))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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))
|
||||
Reference in new issue
Block a user