Lisp - Creating Vectors
A vector is a single dimensional array. A vector can be of fixed size as well as of adjustable length. In this chapter, we're discussing various functions available in LISP to create vectors.
Creating Vector using make-array function
make-array is a generic function to create various types of vectors.
Create a vector of 10 integers
; define a vector of 10 integers (defvar my-vector (make-array 10 :element-type 'integer)) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)
Create a vector of 5 elements
; define a vector of 5 elements (defvar my-vector (make-array 5)) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(NIL NIL NIL NIL NIL)
Create a vector of 5 elements initialized with 0
; define a vector of 5 elements initialized with 0 (defvar my-vector (make-array 5 :initial-element 0)) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(0 0 0 0 0)
Create a vector with list of values
; define a vector of 3 elements with given list of values (defvar my-vector (make-array 3 :initial-contents '(1 2 3))) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(1 2 3)
Create a vector with values
; define a vector of 3 elements with given list of values (defvar my-vector (make-array 3 :initial-contents '(1 2 3))) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(1 2 3)
Create an adjustable vector
; define an adjustable vector (defvar my-vector(make-array 5 :adjustable t :fill-pointer 0 :initial-element 0)) ; print the Vector (print my-vector) (terpri) ; get and print the fill-Pointer (print (fill-pointer my-vector))
Output
When you execute the code, it returns the following result −
#() 0
Creating Vector using vector function
vector is a specific construct to create vector with values.
Create a vector of 3 given integers
; define a vector of 3 numbers (defvar my-vector (vector 1 2 3)) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(1 2 3)
Create a vector of 3 given strings
; define a vector of 3 strings (defvar my-vector (vector "apple" "banana" "orange")) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#("apple" "banana" "orange")
Create a vector of 3 given characters
; define a vector of 3 characters (defvar my-vector (vector 'a 'b 'c)) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(A B C)
Create vector directly using values.
We can create vector directly using # as shown in examples below
Create a vector of 3 given integers
; define a vector of 3 numbers (defvar my-vector #(1 2 3)) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(1 2 3)
Create a vector of 3 given strings
; define a vector of 3 strings
(defvar my-vector #("apple" "banana" "orange"))
; print the vector
(print my-vector)
Output
When you execute the code, it returns the following result −
#("apple" "banana" "orange")
Create a vector of 3 given characters
; define a vector of 3 characters (defvar my-vector #(a b c)) ; print the vector (print my-vector)
Output
When you execute the code, it returns the following result −
#(A B C)