LISP Vectors - Lisp programming

What are Vectors in LISP?

The Vectors are one-dimensional arrays, a subtype of array. Vectors and lists are collectively called sequences.

Creating Vectors

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

Example 1

The file named main.lisp is created and the following code is included in it.

On executing the above code the following result is produced:

  • In this, LISP uses the #(...) syntax as the literal notation for vectors.
  • This #(... ) syntax is used to create and include literal vectors in your code.
  • Modifying the literal vectors is not defined in LISP.
  • For programming, the vector function, or the more general function make-array are always used 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 2

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

On executing the above code the following result is produced:

Fill Pointer

  • The make-array function is used 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:

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

On executing the above code the following result is produced:

The Vectors being sequences, all sequence functions are applicable for vectors.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Lisp programming Topics