Open In App

Sequences in LISP

Last Updated : 16 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads