
- 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 - Sequences
Sequence is an abstract data type in LISP. Vectors and lists are the two concrete subtypes of this data type. All the functionalities defined on sequence data type are actually applied on all vectors and list types.
In this section, we will discuss most commonly used functions on sequences.
Before starting on various ways of manipulating sequences (i.e., vectors and lists), let us have a look at the list of all available functions.
Creating a Sequence
The function make-sequence allows you to create a sequence of any type. The syntax for this function is −
make-sequence sqtype sqsize &key :initial-element
It creates a sequence of type sqtype and of length sqsize.
You may optionally specify some value using the :initial-element argument, then each of the elements will be initialized to this value.
For example, Create a new source code file named main.lisp and type the following code in it.
main.lisp
; create a sequence of 10 floating numbers initilized with 1.0 (write (make-sequence '(vector float) 10 :initial-element 1.0))
Output
When you execute the code, it returns the following result −
#(1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0)
Generic Functions on Sequences
Sr.No. | Function & Description |
---|---|
1 |
elt It allows access to individual elements through an integer index. |
2 |
length It returns the length of a sequence. |
3 |
subseq It returns a sub-sequence by extracting the subsequence starting at a particular index and continuing to a particular ending index or the end of the sequence. |
4 |
copy-seq It returns a sequence that contains the same elements as its argument. |
5 |
fill It is used to set multiple elements of a sequence to a single value. |
6 |
replace It takes two sequences and the first argument sequence is destructively modified by copying successive elements into it from the second argument sequence. |
7 |
count It takes an item and a sequence and returns the number of times the item appears in the sequence. |
8 |
reverse It returns a sequence contains the same elements of the argument but in reverse order. |
9 |
nreverse It returns the same sequence containing the same elements as sequence but in reverse order. |
10 |
concatenate It creates a new sequence containing the concatenation of any number of sequences. |
11 |
position It takes an item and a sequence and returns the index of the item in the sequence or nil. |
12 |
find It takes an item and a sequence. It finds the item in the sequence and returns it, if not found then it returns nil. |
13 |
sort It takes a sequence and a two-argument predicate and returns a sorted version of the sequence. |
14 |
merge It takes two sequences and a predicate and returns a sequence produced by merging the two sequences, according to the predicate. |
15 |
map It takes an n-argument function and n sequences and returns a new sequence containing the result of applying the function to subsequent elements of the sequences. |
16 |
some It takes a predicate as an argument and iterates over the argument sequence, and returns the first non-NIL value returned by the predicate or returns false if the predicate is never satisfied. |
17 |
every It takes a predicate as an argument and iterate over the argument sequence, it terminates, returning false, as soon as the predicate fails. If the predicate is always satisfied, it returns true. |
18 |
notany It takes a predicate as an argument and iterate over the argument sequence, and returns false as soon as the predicate is satisfied or true if it never is. |
19 |
notevery It takes a predicate as an argument and iterate over the argument sequence, and returns true as soon as the predicate fails or false if the predicate is always satisfied. |
20 |
reduce It maps over a single sequence, applying a two-argument function first to the first two elements of the sequence and then to the value returned by the function and subsequent elements of the sequence. |
21 |
search It searches a sequence to locate one or more elements satisfying some test. |
22 |
remove It takes an item and a sequence and returns the sequence with instances of item removed. |
23 |
delete This also takes an item and a sequence and returns a sequence of the same kind as the argument sequence that has the same elements except the item. |
24 |
substitute It takes a new item, an existing item, and a sequence and returns a sequence with instances of the existing item replaced with the new item. |
25 |
nsubstitute It takes a new item, an existing item, and a sequence and returns the same sequence with instances of the existing item replaced with the new item. |
26 |
mismatch It takes two sequences and returns the index of the first pair of mismatched elements. |
Standard Sequence Function Keyword Arguments
Argument | Meaning | Default Value |
---|---|---|
:test | It is a two-argument function used to compare item (or value extracted by :key function) to element. | EQL |
:key | One-argument function to extract key value from actual sequence element. NIL means use element as is. | NIL |
:start | Starting index (inclusive) of subsequence. | 0 |
:end | Ending index (exclusive) of subsequence. NIL indicates end of sequence. | NIL |
:from-end | If true, the sequence will be traversed in reverse order, from end to start. | NIL |
:count | Number indicating the number of elements to remove or substitute or NIL to indicate all (REMOVE and SUBSTITUTE only). | NIL |
We have just discussed various functions and keywords that are used as arguments in these functions working on sequences. In the next sections, we will see how to use these functions using examples.
Finding Length and Element
The length function returns the length of a sequence, and the elt function allows you to access individual elements using an integer index.
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; set x as a vector of 5 characters (setq x (vector 'a 'b 'c 'd 'e)) ; print length of x (write (length x)) ; terminate printing (terpri) ; print value at index 3 (write (elt x 3))
Output
When you execute the code, it returns the following result −
5 D
Modifying Sequences
Some sequence functions allows iterating through the sequence and perform some operations like, searching, removing, counting or filtering specific elements without writing explicit loops.
The following example demonstrates this −
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; print count of 7 in the list (write (count 7 '(1 5 6 7 8 9 2 7 3 4 5))) ; terminate printing (terpri) ; remove occurences of 5 from the list (write (remove 5 '(1 5 6 7 8 9 2 7 3 4 5))) ; terminate printing (terpri) ; delete occurences of 5 from the list (write (delete 5 '(1 5 6 7 8 9 2 7 3 4 5))) ; terminate printing (terpri) ; substitute 7 with 10 in the list (write (substitute 10 7 '(1 5 6 7 8 9 2 7 3 4 5))) ; terminate printing (terpri) ; find index of 7 in the list (write (find 7 '(1 5 6 7 8 9 2 7 3 4 5))) ; terminate printing (terpri) ; find position of 5 in the list (write (position 5 '(1 5 6 7 8 9 2 7 3 4 5)))
Output
When you execute the code, it returns the following result −
2 (1 6 7 8 9 2 7 3 4) (1 6 7 8 9 2 7 3 4) (1 5 6 10 8 9 2 10 3 4 5) 7 1
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; delete odd numbers from the list (write (delete-if #'oddp '(1 5 6 7 8 9 2 7 3 4 5))) ; terminate printing (terpri) ; delete even numbers from the list (write (delete-if #'evenp '(1 5 6 7 8 9 2 7 3 4 5))) ; terminate printing (terpri) ; remove last even number from the list (write (remove-if #'evenp '(1 5 6 7 8 9 2 7 3 4 5) :count 1 :from-end t)) ; terminate printing (terpri) ; set x as vector of characters (setq x (vector 'a 'b 'c 'd 'e 'f 'g)) ; fill x with p starting from 1 to 4 indexes excluding (fill x 'p :start 1 :end 4) ; print x (write x)
Output
When you execute the code, it returns the following result −
(6 8 2 4) (1 5 7 9 7 3 5) (1 5 6 7 8 9 2 7 3 5) #(A P P P E F G)
Sorting and Merging Sequences
The sorting functions take a sequence and a two-argument predicate and return a sorted version of the sequence.
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; sort and print the list (write (sort '(2 4 7 3 9 1 5 4 6 3 8) #'<)) ; terminate printing (terpri) ; sort in reverse order and print the list (write (sort '(2 4 7 3 9 1 5 4 6 3 8) #'>)) ; terminate printing (terpri)
Output
When you execute the code, it returns the following result −
(1 2 3 3 4 4 5 6 7 8 9) (9 8 7 6 5 4 4 3 3 2 1)
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; merge two vectors in ascending order and print (write (merge 'vector #(1 3 5) #(2 4 6) #'<)) ; terminate printing (terpri) ; merge two lists in ascending order and print (write (merge 'list #(1 3 5) #(2 4 6) #'<)) ; terminate printing (terpri)
Output
When you execute the code, it returns the following result −
#(1 2 3 4 5 6) (1 2 3 4 5 6)
Sequence Predicates
The functions every, some, notany, and notevery are called the sequence predicates.
These functions iterate over sequences and test the Boolean predicate.
All these functions takes a predicate as the first argument and the remaining arguments are sequences.
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; check if all numbers are even (write (every #'evenp #(2 4 6 8 10))) ; terminate printing (terpri) ; check if some numbers are even (write (some #'evenp #(2 4 6 8 10 13 14))) ; terminate printing (terpri) ; check if all numbers are even (write (every #'evenp #(2 4 6 8 10 13 14))) ; terminate printing (terpri) ; check if any numbers is not even (write (notany #'evenp #(2 4 6 8 10))) ; terminate printing (terpri) ; check if all numbers are not even (write (notevery #'evenp #(2 4 6 8 10 13 14))) ; terminate printing (terpri)
Output
When you execute the code, it returns the following result −
T T NIL NIL T
Mapping Sequences
We have already discussed the mapping functions. Similarly the map function allows you to apply a function on to subsequent elements of one or more sequences.
The map function takes a n-argument function and n sequences and returns a new sequence after applying the function to subsequent elements of the sequences.
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; compute multiple of vectors and print (write (map 'vector #'* #(2 3 4 5) #(3 5 4 8)))
Output
When you execute the code, it returns the following result −
#(6 15 16 40)