# List Manipulation in LISP

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:

1. The first argument will be the value.
2. 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)```

Previous
Next