List Manipulation in LISP
Last Updated :
22 Nov, 2021
In this article, we will discuss List manipulation in LISP. The list is a data structure that can store elements of multiple data type
we can create a list by using the list function
Syntax:
(list element1 element2 .................. element n)
List manipulating functions are used to manipulate and return the list of elements
1. car() Function:
This function is used to display the first element in the list
Syntax:
(car '(element1 element2 ........... element n))
Example:
Lisp
;list of integer elements
(write (car '(1 2 3 4 5)))
(terpri)
;list of characters
(write (car '(a b)))
(terpri)
Output:
1
A
2. cdr() Function:
This function is used to display the list of all elements excluding the first element
Syntax:
(cdr '(element1 element2 ........... element n))
Example:
Lisp
;list of integer elements
(write (cdr '(1 2 3 4 5)))
(terpri)
;list of characters
(write (cdr '(a b)))
(terpri)
Output:
(2 3 4 5)
(B)
3.cons() FUnction:
This function will take two items. one is a value and another is the list. This will insert the value into first place in the list and returns the new list
Syntax:
(cons 'value '(list_of_elements)))
where,
- value is the element to be inserted
- list_of_elements is the list of elements
Example:
Lisp
;list of integer elements
;insert 100
(write (cons '100 '(1 2 3 4 5)))
(terpri)
;list of characters
;insert a
(write (cons 'a '(b c d e f)))
(terpri)
Output:
(100 1 2 3 4 5)
(A B C D E F)
4. list() Function:
This function will take multiple lists and display it
Syntax:
(list 'list1 'list2 ........ 'listn)
Example:
Lisp
;two lists and one variable
(write (list '100 '(1 2 3 4 5) '(g e e k s)))
(terpri)
Output:
(100 (1 2 3 4 5) (G E E K S))
5. append() Function:
This function will append the two or multiple lists.
Syntax:
(append'list1 'list2 ........ 'listn)
Example:
Lisp
;consider two lists and append
(write (append '(1 2 3 4 5) '(g e e k s)))
(terpri)
;consider three lists and append
(write (append '(10 20) '(20 30 45) '(a s d)))
(terpri)
Output:
(1 2 3 4 5 G E E K S)
(10 20 20 30 45 A S D)
6. last() Function:
This function returns the list that contains the last element in the list
Syntax:
(last 'list)
Example:
Lisp
;consider a list and get last element
(write (last '(1 2 3 4 5) ))
(terpri)
;consider a list and get last element
(write (last '(10 20 ( g e e k s))))
(terpri)
Output:
(5)
((G E E K S))
7. member() Function:
This function is used to return the list based on members given.
It will take two arguments:
- The first argument will be the value.
- The second argument will be the list.
If the value is present in the list, then it will return the list
Otherwise, NIL is returned.
Syntax:
(member 'value 'list )
Example:
Lisp
;consider a list with 1 as member
(write (member '1 '(1 2 3 4 5) ))
(terpri)
;consider a list with 10 as member
(write (member '10 '(1 2 3 4 5) ))
(terpri)
Output:
(1 2 3 4 5)
NIL
8. reverse() Function:
This function reverses the list and returns the reversed list
Syntax:
(reverse 'list)
Example:
Lisp
;consider a list and reverse
(write (reverse '(1 2 3 4 5) ))
(terpri)
;consider a list nnd reverse
(write (reverse '(g e e k s) ))
(terpri)
Output:
(5 4 3 2 1)
(S K E E G)
Similar Reads
Property Lists in LISP In Lisp, every symbol has a property list (plist). When a symbol is created initially its property list is empty. A property list consists of entries where every entry consists of a key called an indicator and a value called a property . There are no duplicates among the indicators. In contrast to a
3 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
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
Input & Output in LISP Pre-requisites: Introduction to LISP Lisp provides a huge set of facilities for performing input/output. All the input/output operations are performed on streams of several kinds. While reading and writing binary data is possible, the majority of Common Lisp input/output methods read or write charac
9 min read
Operators in LISP Operators are the foundation of any programming language. Thus the functionality of the LISP programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can
5 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