
- LISP Tutorial
- LISP - Home
- LISP - Overview
- LISP - Environment
- LISP - REPL
- LISP - Program Structure
- LISP - Basic Syntax
- LISP - Data Types
- Lisp Macros
- LISP - Macros
- LISP - Backquote and Comma
- LISP - Code Generation Using Macro
- LISP - Variable Capture and Hygienic macro
- LISP - Scope and Binding
- LISP - Macro Writing Style
- LISP - Macro Characters
- LISP - Read-Time Macros
- LISP - Compiler Macros
- LISP - Uses of Macros
- Lisp Functions
- LISP - Functions
- LISP - Functions vs Macros
- LISP - Calling Function using funcall
- LISP - Calling Function using apply
- LISP - Closures
- LISP - Functions as Arguments
- LISP - Functions as Return Values
- LISP - Recursion
- LISP - Built-in Functions
- Lisp Predicates
- LISP - Predicates
- LISP - Generic Data Type Predicates
- LISP - Specific Data Type Predicates
- LISP - Equality Predicates
- LISP - Numeric Predicates
- LISP - Comparison Predicates
- LISP - Logical Predicates
- LISP - List Predicates
- LISP - Custom Predicates
- LISP - Chaining Predicates
- Lisp Arrays
- LISP - Arrays
- LISP - Adjustable Arrays
- LISP - Fill Pointers in Arrays
- LISP - Specialized Arrays
- LISP - Arrays Properties
- LISP - Iterating over Arrays
- LISP - Multidimensional Arrays
- LISP - Row-Major Order
- Lisp Strings
- LISP - Strings
- LISP - String Concatenation
- LISP - String Comparison
- LISP - String Case Conversion
- LISP - String Trimmimg
- LISP - String Searching
- LISP - Getting Substring
- LISP - String Replacement
- LISP - Sorting Strings
- LISP - Merging Strings
- LISP - Accessing Characters of String
- LISP - String length
- LISP - Escape Sequences
- Lisp Sequences
- LISP - Sequences
- LISP - Accessing Element of Sequence
- LISP - Sequence length
- LISP - Getting Subsequence
- LISP - Search Element in Sequence
- LISP - Sequence Concatenation
- LISP - Reversing a Sequence
- LISP - Mapping Sequence Element
- LISP - position of Element
- LISP - Remove an Element
- LISP - Sort Sequence
- LISP - Merge Sequences
- LISP - every function
- LISP - some function
- LISP - notany function
- LISP - notevery function
- Lisp Lists
- LISP - Lists
- LISP - Accessing Elements of Lists
- LISP - Modifications to Lists
- LISP - Using mapcar on List
- LISP - Using mapc on List
- LISP - Using reduce on List
- LISP - Removing elements from List
- LISP - Reversing a List
- LISP - Sorting a List
- LISP - Searching a List
- LISP - List vs Vectors
- LISP - Matrix Multiplication
- Lisp Vectors
- LISP - Vectors
- LISP - Creating Vectors
- LISP - Accessing Elements of Vectors
- LISP - Modifications to Vectors
- LISP - Adjustable Vectors
- LISP - Specialized Vectors
- LISP - Vector Functions
- Lisp Set
- LISP - Set
- LISP - Adding elements to the Set
- LISP - Getting SubSet from a Set
- LISP - Set Difference
- LISP - Set Exclusive OR
- LISP - Set Intersection
- LISP - Set Union
- LISP - Representing Set with HashTable
- LISP - List as Set vs HashTable as Set
- Lisp Tree
- LISP - Tree
- LISP - Recursive Traversal
- LISP - Inorder Traversal
- LISP - Preorder Traversal
- LISP - Postorder Traversal
- LISP - Depth First Traversal
- LISP - Modifying Tree
- LISP - Search Tree
- LISP - Binary Tree
- Lisp Hash Table
- LISP - Hash Table
- Adding Values to Hash Table
- Removing Values from Hash Table
- Updating Values of Hash Table
- Iterating Hash Table Entries
- Searching key in HashTable
- Checking Size of HashTable
- Using Custom Equality Check
- Lisp - Input − Output
- LISP - Input − Output
- LISP - Streams
- LISP - Reading Data from Streams
- LISP - Writing Data to Streams
- LISP - File I/O
- LISP - String I/O
- LISP - Formatting with Format
- LISP - Interactive I/O
- LISP - Error Handling
- LISP - Binary I/O
- Lisp - Structures
- LISP - Structures
- LISP - Accessors and Mutators
- LISP - Structure Options
- LISP - Structure Types
- LISP - Applications and Best Practices
- Lisp - CLOS
- LISP - CLOS
- Lisp - Objects
- LISP - Class
- LISP - Slots and Accessors
- LISP - Generic Functions
- LISP - Class Precedence
- LISP - Metaobject Protocol
- LISP - Multimethods
- LISP - Multiple Inheritance
- LISP - Method Combinations
- LISP - Method Combinations
- LISP - :before Method Combination
- LISP - :primary Method Combination
- LISP - :after Method Combination
- LISP - :around Method Combination
- LISP - + Method Combination
- LISP - and Method Combination
- LISP - append Method Combination
- LISP Useful Resources
- Lisp - Quick Guide
- Lisp - Useful Resources
- Lisp - Discussion
Lisp - Arrays
LISP allows you to define single or multiple-dimension arrays using the make-array function. An array can store any LISP object as its elements.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

The number of dimensions of an array is called its rank.
In LISP, an array element is specified by a sequence of non-negative integer indices. The length of the sequence must equal the rank of the array. Indexing starts from zero.
For example, to create an array with 10- cells, named my-array, we can write −
(setf my-array (make-array '(10)))
The aref function allows accessing the contents of the cells. It takes two arguments, the name of the array and the index value.
For example, to access the content of the tenth cell, we write −
(aref my-array 9)
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; create an empty array of size 10 and print it (write (setf my-array (make-array '(10)))) ; terminate printing (terpri) ; set value at each index of the array (setf (aref my-array 0) 25) (setf (aref my-array 1) 23) (setf (aref my-array 2) 45) (setf (aref my-array 3) 10) (setf (aref my-array 4) 20) (setf (aref my-array 5) 17) (setf (aref my-array 6) 25) (setf (aref my-array 7) 19) (setf (aref my-array 8) 67) (setf (aref my-array 9) 30) ; print the updated array (write my-array)
Output
When you execute the code, it returns the following result −
#(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) #(25 23 45 10 20 17 25 19 67 30)
Example
Let us create a 3-by-3 array.
Update the source code file named main.lisp and type the following code in it.
main.lisp
; create an array of 3 x 3 (setf x (make-array '(3 3) ; initialize the array :initial-contents '((0 1 2 ) (3 4 5) (6 7 8))) ) ; print the array (write x)
Output
When you execute the code, it returns the following result −
#2A((0 1 2) (3 4 5) (6 7 8))
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; assign a an array of 4 x 3 (setq a (make-array '(4 3))) ; loop 4 times (dotimes (i 4) ; loop 3 times (dotimes (j 3) ; set the values at a index i, j (setf (aref a i j) (list i 'x j '= (* i j))) ) ) ; loop 4 times (dotimes (i 4) ; loop 3 times (dotimes (j 3) ; print values of array (print (aref a i j)) ) )
Output
When you execute the code, it returns the following result −
(0 X 0 = 0) (0 X 1 = 0) (0 X 2 = 0) (1 X 0 = 0) (1 X 1 = 1) (1 X 2 = 2) (2 X 0 = 0) (2 X 1 = 2) (2 X 2 = 4) (3 X 0 = 0) (3 X 1 = 3) (3 X 2 = 6)
Complete Syntax for the make-array Function
The make-array function takes many other arguments. Let us look at the complete syntax of this function −
make-array dimensions :element-type :initial-element :initial-contents :adjustable :fill-pointer :displaced-to :displaced-index-offset
Apart from the dimensions argument, all other arguments are keywords. The following table provides brief description of the arguments.
Sr.No. | Argument & Description |
---|---|
1 |
dimensions It gives the dimensions of the array. It is a number for one-dimensional array, and a list for multi-dimensional array. |
2 |
:element-type It is the type specifier, default value is T, i.e. any type |
3 |
:initial-element Initial elements value. It will make an array with all the elements initialized to a particular value. |
4 |
:initial-content Initial content as object. |
5 |
:adjustable It helps in creating a resizeable (or adjustable) vector whose underlying memory can be resized. The argument is a Boolean value indicating whether the array is adjustable or not, default value being NIL. |
6 |
:fill-pointer It keeps track of the number of elements actually stored in a resizeable vector. |
7 |
:displaced-to It helps in creating a displaced array or shared array that shares its contents with the specified array. Both the arrays should have same element type. The :displaced-to option may not be used with the :initial-element or :initial-contents option. This argument defaults to nil. |
8 |
:displaced-index-offset It gives the index-offset of the created shared array. |
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; set myarray as an array (setq myarray (make-array '(3 2 3) ; set the contents of the array :initial-contents '(((a b c) (1 2 3)) ((d e f) (4 5 6)) ((g h i) (7 8 9)) )) ) ; set array2 using myarray as one dimensional array (setq array2 (make-array 4 :displaced-to myarray :displaced-index-offset 2)) ; print myarray (write myarray) ; terminate printing (terpri) ; print array2 (write array2)
Output
When you execute the code, it returns the following result −
#3A(((A B C) (1 2 3)) ((D E F) (4 5 6)) ((G H I) (7 8 9))) #(C 1 2 3)
If the displaced array is two dimensional −
main.lisp
; set myarray as an array (setq myarray (make-array '(3 2 3) ; set the contents of the array :initial-contents '(((a b c) (1 2 3)) ((d e f) (4 5 6)) ((g h i) (7 8 9)) )) ) ; set array2 using myarray as two dimensional array (setq array2 (make-array '(3 2) :displaced-to myarray :displaced-index-offset 2)) ; print myarray (write myarray) ; terminate printing (terpri) ; print array2 (write array2)
Output
When you execute the code, it returns the following result −
#3A(((A B C) (1 2 3)) ((D E F) (4 5 6)) ((G H I) (7 8 9))) #2A((C 1) (2 3) (D E))
Let's change the displaced index offset to 5 −
main.lisp
; set myarray as an array (setq myarray (make-array '(3 2 3) ; set the contents of the array :initial-contents '(((a b c) (1 2 3)) ((d e f) (4 5 6)) ((g h i) (7 8 9)) )) ) ; set array2 using myarray as two dimensional array with offset 5 (setq array2 (make-array '(3 2) :displaced-to myarray :displaced-index-offset 5)) ; print myarray (write myarray) ; terminate printing (terpri) ; print array2 (write array2)
Output
When you execute the code, it returns the following result −
#3A(((A B C) (1 2 3)) ((D E F) (4 5 6)) ((G H I) (7 8 9))) #2A((3 D) (E F) (4 5))
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
;a one dimensional array with 5 elements, ;initail value 5 (write (make-array 5 :initial-element 5)) (terpri) ;two dimensional array, with initial element a (write (make-array '(2 3) :initial-element 'a)) (terpri) ;an array of capacity 14, but fill pointer 5, is 5 (write(length (make-array 14 :fill-pointer 5))) (terpri) ;however its length is 14 (write (array-dimensions (make-array 14 :fill-pointer 5))) (terpri) ; a bit array with all initial elements set to 1 (write(make-array 10 :element-type 'bit :initial-element 1)) (terpri) ; a character array with all initial elements set to a ; is a string actually (write(make-array 10 :element-type 'character :initial-element #\a)) (terpri) ; a two dimensional array with initial values a (setq myarray (make-array '(2 2) :initial-element 'a :adjustable t)) (write myarray) (terpri) ;readjusting the array (adjust-array myarray '(1 3) :initial-element 'b) (write myarray)
Output
When you execute the code, it returns the following result −
#(5 5 5 5 5) #2A((A A A) (A A A)) 5 (14) #*1111111111 "aaaaaaaaaa" #2A((A A) (A A)) #2A((A A B))