Lisp - Vectors



Vectors are one-dimensional arrays, therefore a subtype of array. Vectors and lists are collectively called sequences. Therefore all sequence generic functions and array functions we have discussed so far, work on vectors.

Creating Vectors

The vector function allows you to make fixed-size vectors with specific values. It takes any number of arguments and returns a vector containing those arguments.

Example

Create a new source code file named main.lisp and type the following code in it.

main.lisp

; create and assign a vector to v1
(setf v1 (vector 1 2 3 4 5))
; create and assign a vector to v2
(setf v2 #(a b c d e))
; create and assign a vector to v3
(setf v3 (vector 'p 'q 'r 's 't))

; print v1
(write v1)
; terminate printing
(terpri)
; print v2
(write v2)
; terminate printing
(terpri)
; print v3
(write v3)

Output

When you execute the code, it returns the following result −

#(1 2 3 4 5)
#(A B C D E)
#(P Q R S T)

Please note that LISP uses the #(...) syntax as the literal notation for vectors. You can use this #(... ) syntax to create and include literal vectors in your code.

However, these are literal vectors, so modifying them is not defined in LISP. Therefore, for programming, you should always use the vector function, or the more general function make-array to create vectors you plan to modify.

The make-array function is the more generic way to create a vector. You can access the vector elements using the aref function.

Example

Update the source code file named main.lisp and type the following code in it.

main.lisp

; create and assign an array of size 5 initilized with 0 values
(setq a (make-array 5 :initial-element 0))
; create and assign an array of size 5 initilized with 2 values
(setq b (make-array 5 :initial-element 2))

; loop for 5 times
(dotimes (i 5)
   ; set an array values
   (setf (aref a i) i))

; print a   
(write a)
;terminate printing
(terpri)
; print b
(write b)
; terminate printing
(terpri)

Output

When you execute the code, it returns the following result −

#(0 1 2 3 4)
#(2 2 2 2 2)

Fill Pointer

The make-array function allows you to create a resizable vector.

The fill-pointer argument of the function keeps track of the number of elements actually stored in the vector. It's the index of the next position to be filled when you add an element to the vector.

The vector-push function allows you to add an element to the end of a resizable vector. It increases the fill-pointer by 1.

The vector-pop function returns the most recently pushed item and decrements the fill pointer by 1.

Example

Update the source code file named main.lisp and type the following code in it.

main.lisp

; create and assign an array of size 5 initilized with 0 values
(setq a (make-array 5 :fill-pointer 0))
; print a
(write a)
; add a to the vector a
(vector-push 'a a)
; add b to the vector a
(vector-push 'b a)
; add c to the vector a
(vector-push 'c a)
; terminate printing
(terpri)
; print vector a
(write a)
; terminate printing
(terpri)
; add d to the vector a
(vector-push 'd a)
; add e to the vector a
(vector-push 'e a)

;this will not be entered as the vector limit is 5
(vector-push 'f a)
; print vector a
(write a)
; terminate printing
(terpri)

; remove last pushed element
(vector-pop a)
; remove last pushed element
(vector-pop a)
; remove last pushed element
(vector-pop a)
; print vector
(write a)

Output

When you execute the code, it returns the following result −

#()
#(A B C)
#(A B C D E)
#(A B)

Vectors being sequences, all sequence functions are applicable for vectors. Please consult the sequences chapter, for vector functions.

Advertisements