In Lisp, the ordered set of elements is represented by sequences. All the functionality we use in sequences is applied on vectors and lists which are two of the subtypes of sequences.
Creating a Sequence:
The generic function for creating a Sequence in Lisp is:
;The generic function for creating a Sequence in Lisp make-sequence type size &key:default-element
Note:
In the above syntax, type represents the type
of sequence and size represents the size of the sequence.
The :default-element sets all the elements in the
sequence initialised to the given value
Example 1:
Lisp
; Creating a vector of size 6 with 2
; as the default element of type fixnum or integer
(write (make-sequence '(vector fixnum)
6 :initial-element 2))
Output:
 Generic Functions on Sequences:
In Lisp, we have a lot of generic functions which can be applied to vectors and lists.
S.No. | Function | Description |
---|
1 | elt | Provides access to individual elements through an integer index. |
2 | length | Finds the length of the sequence. |
3 | fill | Used to set multiple elements to a single value |
4 | copy-seq | Returns a sequence that contains the same elements as its argument |
5 | subseq | Returns a subsequence from one index to another or the end of the sequence. |
6 | count | Returns the number of times an item appears in a sequence. |
7 | reverse | Returns a sequence that contains elements of a sequence in reverse order. |
8 | replace | It consists of two sequences in which the elements of the second sequence are copied to the first one. |
9 | concatenate | Creates a new sequence that contains a concatenation of two or more sequences. |
10 | nreverse | Returns the same sequence with the element in reverse order. |
11 | sort | Returns a sequence in sorted order with a two-order predicate, |
12 | find | It finds an item in a sequence if the item is not found, then it returns nil. |
13 | position | It finds and returns the index of an item in a sequence, if not present it returns nil. |
14 | some | It takes a predicate as an argument and it iterates over the sequence, finds the first non-nil argument, and returns it, if the predicate is not satisfied at all returns false. |
15 | map | It takes a function and multiple sequences and returns a new sequence that contains the result after applying the function to subsequent elements of all the sequences. |
16 | merge | Merges the sequences together according to the predicate. |
17 | every | It takes a predicate as an argument and returns true if the predicate is always satisfied else returns false. |
18 | notany | It returns true if the passed predicate is never satisfied, else returns false. |
19 | notevery | It returns true if some of the sequence elements do not satisfy the predicate, if all of them satisfy then returns false. |
20 | reduce | It reduces the length of a sequence by 1 unit by applying a 2 argument function on the first two elements, the value obtained is returned along with the subsequent elements. |
21 | search | Searches for elements in a sequence that satisfy a condition or test. |
22 | remove | Removes all the instances of an element from a sequence, the instance, and sequence are passed as parameters. |
23 | delete | Deletes all the instances of an element from a sequence, the instance, and sequence are passed as parameters. |
24 | mismatch | Takes two sequences and returns the first index where the elements of the two sequences mismatch. |
25 | nsubstitute | Takes a new item and existing item as arguments and returns the same sequence where the new item replaces the old item. |
26 | substitute | Takes a new item and existing item as arguments and returns a sequence where the new item replaces the old item. |
Standard Sequence Function Keyword Arguments:
S.No. | Argument | Meaning | Default Value |
---|
1 | :key |
It is a one-argument function that extracts the key value from the sequence elements
NIL is an indication to use the element as is.
| NIL
|
---|
2 | :test | A two-argument function that is used to compare the elements | EQL
|
---|
3 | :start | Starting index (inclusive) of a sequence | 0
|
---|
4 | :count | The number indicates the number of elements to remove or substitute while NIL indicates all | NIL
|
---|
5 | :from-end | If it is true then the sequence is traversed in reverse order. | NIL
|
---|
6 | :end | Ending index (exclusive) of the sequence. NIL represents the end of the sequence | NIL
|
---|
Since now we are familiar with the generic and standard sequence functions let us now implement these in some examples.
Finding Length and Element in Sequences:
As we discussed the generic functions, we can easily find the length and a particular element in a sequence. Recall that length was used to find the length whereas we will use the elt function to find a particular element.
Example 2:
Lisp
; Finding the length of a vector and element at an index
(setq x (vector 'kishan 'rohan 'vishal 'sarthak))
(write (length x))
(terpri)
(write (elt x 3))
Output:
 Note:
Indexing in Lisp is also zero-based.
Modifying Sequences in Lisp:
For performing any modification in a sequence we have some pre-defined functions for removing, counting, searching, and filtering.
Example 3:
Lisp
; Sequence Modification Functions
(write (count 8 '(2 8 8 8 8 3 8 1 6 8 9)))
(terpri)
(write (remove 5 '(1 5 6 7 4 5)))
(terpri)
(write (substitute 0 9 '(1 5 6 7 8 9 2 7 3 4 5)))
(terpri)
(write (find 6 '(1 5 6 7 8 2 9 5)))
(terpri)
(write (position 9 '(1 7 5 6 8 9 5)))
Output:
 Also, we can do Conditional Modification that is we can put some conditions and perform operations on the list.Â
Example 4:
Lisp
; Conditional Modification
(write (delete-if #'oddp '(1 2 3 4 5 6)))
(terpri)
(write (delete-if #'evenp '(1 2 3 4 5 6)))
(terpri)
(write (remove-if #'evenp '(1 2 3 4 5 6) :count 2 :from-end t))
(terpri)
(setq x (vector 'apple 'apple 'apple 'guava))
(fill x 'mango :start 0 :end 2)
(write x)
Output:
 Sorting and Merging Sequences in Lisp:
Lisp provides us with operations with which we can sort a sequence and also can merge sequences together. The functions sort and merge do the operations respectively.
- Sorting Sequences: For sorting, we use the sort function, the order is provided after the list so as to sort in some specific manner.
Example 5:
Lisp
; Sorting Sequences
; A) Ascending Order
(write (sort '(8 7 6 5 3 4 1 2) #'<))
(terpri)
; B) Descending Order
(write (sort '(1 2 3 4 6 5 7 9 8) #'>))
(terpri)
Output:
 - Merging Sequences: For merging sequences we use the merge function, the order is provided after the list so as to merge them in some specific manner.
Example 6:
Lisp
; Merging Sequences
; A) Merging Vectors
(write (merge 'vector #(1 3 5) #(2 4 6) #'<))
(terpri)
; B) Merging Lists
(write (merge 'list #(1 3 5) #(2 4 6) #'<))
(terpri)
Output:
 - Sequence Predicates: These are the functions that iterate over the sequence and test the boolean predicates. Hence the first argument is always a predicate to them while the remaining arguments are sequences. They return T(true) else nil(false). Examples: every, some, not many, and not every.
- every: Returns true if all elements follow the condition or not.
- some: Returns true if some elements follow the condition or not.
- not any: Â Returns true if no element follows the condition.
- not every: Returns true if some element does not follow the condition.
 Example 7:
Lisp
;Sequence Predicates in Lisp
(write (some #'evenp #(2 4 6 7)))
(terpri)
(write (every #'evenp #(2 4 6 8 10 13 14)))
(terpri)
(write (notany #'evenp #(2 4 6 8 10)))
(terpri)
(write (notevery #'evenp #(2 4 6 8 10 13)))
(terpri)
Output:
 Â
- Mapping Sequences: In Lisp, we have the map function which provides us with the functionality with which we can apply some operations on multiple sequences and return a single new sequence.Â
Example 9:
Lisp
;Map function on two different sequences
(write (map 'vector #'+ #(1 2 3 9) #(5 6 7 10)))
Output:
Â
Similar Reads
Sequences in Maths
In mathematics, a sequence is an ordered list of numbers or objects that follows a specific rule or pattern. Each number in the sequence is called a term, and the position of a term in the sequence is determined by its index.Types of Sequences1. Finite Sequence: A sequence that has a limited number
3 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
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
Vectors in LISP
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)
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
Strings in LISP
A string is a set of characters. String  are enclosed in double-quotes. Example: "hello geek","java","python" etc Example: LISP program to display strings Lisp ;edisplay hello geek (write-line "Hello Geek") ;display (write-line "Welcome to java") Output: Hello Geek Welcome to javaString Comparison
4 min read
Lists in LISP
Lists in common LISP is simply a single Linked list. In LISP, Lists are designed as a chain of records. While talking about record structures in LISP, the concept of Cons is vital. Cons in LISP is a record structure with 2 Â primary components. A cons function takes in 2 arguments and returns a new c
2 min read
Recursion in LISP
In the Lisp programming language, recursion is a commonly used technique for solving problems. Lisp is a functional programming language, which means it is well-suited to recursive solutions. In LISP, recursion is a programming technique in which a function calls itself repeatedly until a certain co
4 min read
Symbols in LISP
Symbols are lisp data objects and every type of symbol object has a name called its print name. Symbol names may contain any combination of letters and numbers, plus some special characters such as hyphens. A symbol can contain any alphabetic, numeric, or any characters except delimiter characters l
4 min read
Sequences and Series Formulas
Sequences and Series Formulas: In mathematics, sequence and series are the fundamental concepts of arithmetic. A sequence is also referred to as a progression, which is defined as a successive arrangement of numbers in an order according to some specific rules. A series is formed by adding the eleme
10 min read