43 lines
1.5 KiB
Common Lisp
43 lines
1.5 KiB
Common Lisp
;;; 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)))
|