40 lines
1.3 KiB
Common Lisp
40 lines
1.3 KiB
Common Lisp
;;; 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))))
|