In this article, we will discuss Vectors in LISP. Vectors in LISP are one-dimensional arrays which are also known as sequences. We can create a vector using vector function and # symbol
Syntax:
variable_name(vector element1 element2 ... element n)
or
variable_name #(element1 element2 ... element n)
Example: LISP Program to create integer and character vectors and display
Lisp
;create a vector with 5 integer elements
(setf data1 (vector 12 34 56 22 34))
;display
(write data1)
(terpri)
;create a vector with 5 integer elements using # symbol
(setf data1 #( 12 34 56 22 34))
;display
(write data1)
(terpri)
;create a vector with 5 character elements
(setf data2 (vector 'g 'e 'e 'k 's))
;display
(write data2)
(terpri)
;create a vector with 5 character elements
(setf data2 #( 'g 'e 'e 'k 's))
;display
(write data2)
Output:
#(12 34 56 22 34)
#(12 34 56 22 34)
#(G E E K S)
#('G 'E 'E 'K 'S)
Vector operations:
In the given vector we can perform two operations. They are:
- push operation
- pop operation
1. Push Operation:
This is used to insert an element into a vector
Syntax:
(vector-push element array_name)
where
- array_name is an input array
- element is an element to be pushed in an array
Example: LISP Program to create an empty array with size 10 and perform the vector push operation
Lisp
;create an empty array with size 10
(setq my_array (make-array 10 :fill-pointer 0))
;display array
(write my_array)
(terpri)
;push number 1 into array
(vector-push 1 my_array)
;push number 2 into array
(vector-push 2 my_array)
;push number 3 into array
(vector-push 3 my_array)
;push number 4 into array
(vector-push 4 my_array)
;display
(write my_array)
Output:
#()
#(1 2 3 4)
2. Pop Operation:
This function will remove the last inserted element at a time
Syntax:
(vector-pop array_name)
where
- array_name is the input array
Example: POP items in an array
Lisp
;create an empty array with size 10
(setq my_array (make-array 10 :fill-pointer 0))
;display array
(write my_array)
(terpri)
;push number 1 into array
(vector-push 1 my_array)
;push number 2 into array
(vector-push 2 my_array)
;push number 3 into array
(vector-push 3 my_array)
;push number 4 into array
(vector-push 4 my_array)
;display
(write my_array)
;pop 1 item
(vector-pop my_array)
(terpri)
;display array
(write my_array)
;pop again 1 item
(vector-pop my_array)
(terpri)
;display array
(write my_array)
Output:
#()
#(1 2 3 4)
#(1 2 3)
#(1 2)
Similar Reads
Vectors in Julia Vectors in Julia are a collection of elements just like other collections like Array, Sets, Dictionaries, etc. Vector are different from Sets because vectors are ordered collections of elements, and can hold duplicate values, unlike sets which require all the elements to be unique. Vectors are one-d
5 min read
List of Vectors in R Vectors are a sequence of elements belonging to the same data type. A list in R, however, comprises of elements, vectors, variables or lists which may belong to different data types. In this article, we will study how to create a list consisting of vectors as elements and how to access, append and d
6 min read
Numbers in LISP LISP is a list processing programming language. It is widely used in the manipulation of data strings. It provides an input and output library. LISP provides a macro system and provides well control structures for the manipulation of data. LISP Math Function:floor:Â floor returns the nearest smalles
2 min read
Predicates in LISP In this article, we will discuss predicates. Predicates are similar to functions that will be used to test their arguments for conditions. They will return NIL if the conditions are not met, if the conditions are met, they will return T. Types of predicates: Below is a list of major Predicates with
5 min read
Structures in LISP LISP, is a list processing, is a programming language widely used in working with data manipulation. Structures are used defines data types, that have the ability to combine with another data type to complete the given task. Attribute used: The defstruct attribute is used to create an instance of a
2 min read