## Manipulating Lists

We've already seen the procedure list that constructs a list out of several items. Here are some more procedures for working with lists.

### Returning the first element of a list: first

The procedure first returns the first element of a list:

`CL-USER > (first '(23 34 45))23`

There are similar procedures second, third, and so on up to tenth for getting later items in a list.

### Returning all but the first element of a list: rest

The procedure rest takes a list and returns the list minus the first element:

`CL-USER > (rest '(23 34 45))(34 45)`

Note that first and rest were traditionally called car and cdr; you may see references to these alternative names in Lisp.

### Returning the nth element of a list: nth

The procedure nth takes a number and a list and returns the nth element of the list, counting the first element as zero:

CL-USER > (nth 1 '(23 34 45))
34

Note that usually Lisp counts things starting from zero; the procedures first and second are exceptions to this, so:

(nth 0 lst) is equivalent to (first lst)

and (nth 1 lst) is equivalent to (second lst).

Finding the length of a list: length

The procedure length returns the length of a list; for example:

```CL-USER > (length '(1 2 3))
3```

### Constructing lists: cons

The procedure cons takes two parameters - an object and a list - and returns a new list with the object added to the front of the list. For example:

```CL-USER > (cons 1 '(2 3 4 5 6))
(1 2 3 4 5 6)```

Note that the first object can itself be a list:

`CL-USER > (cons '(0 1) '(2 3 4 5 6))((0 1) 2 3 4 5 6)`

### Joining lists: append

The procedure append takes any number of lists, and joins them all together into a single list. For example:

```CL-USER > (append '(1 3 5 7) '(2 4 6 8))
(1 3 5 7 2 4 6 8)```

### Reverse a list: reverse

The procedure reverse takes a list and reverses it:

```CL-USER > (reverse '(1 2 3 4))
(4 3 2 1)```

### Combining the list procedures

Using these procedures for manipulating lists, we can define a new procedure to perform just about any operation on lists that we need.

For example, let's define a procedure insert that inserts an item between the first and second items in a list. So:

`CL-USER 1 > (insert 2 '(1 3 4 5 6))`

should give:

`(1 2 3 4 5 6)`

We can do it as follows:

```(defun insert (item lst)
(cons (first lst)
(cons item
(rest lst))))```

### Exercises

#### 1. Swap the first two items in a list

Write a procedure swap to exchange the first two items of a list. Check that:

`(swap '(9 8 7 6))`

gives:

`(8 9 7 6)`

#### 2. Duplicate the first item in a list

Write a procedure dup to duplicate the first item in a list, Check that:

`(dup '(0 1 2 3))`

gives:

`(0 0 1 2 3)`

#### 3. Return a random item from a list

Write a procedure random-elt that returns a random element of a list. For example:

`(random-elt '(11 22 33 44))`

would randomly return one of the four numbers. Hint: use nth and random.

#### 4. Return the last item in a list

Write a procedure last-elt which returns the last item in a list. For example:

`(last-elt '(1 2 3 4))`

should return 4.

Previous: Variables

Next: Strings

blog comments powered by Disqus
{"ft_lang_label":"__label__en","ft_lang_prob":0.7842298,"math_prob":0.7462852,"size":2993,"snap":"2021-21-2021-25","text_gpt3_token_len":854,"char_repetition_ratio":0.17898963,"word_repetition_ratio":0.051369864,"special_character_ratio":0.30337453,"punctuation_ratio":0.08472012,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9820092,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-13T16:51:32Z\",\"WARC-Record-ID\":\"<urn:uuid:18d2f7f1-dc7a-4580-9d5b-034ff185ab8d>\",\"Content-Length\":\"9571\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e17484a2-72a9-4f6e-a77d-19e714ec2b74>\",\"WARC-Concurrent-To\":\"<urn:uuid:4e7f1f2e-a0bd-439f-bf8c-abfdd8b94a2d>\",\"WARC-IP-Address\":\"80.248.178.40\",\"WARC-Target-URI\":\"http://lisp.plasticki.com/show?2EE\",\"WARC-Payload-Digest\":\"sha1:7O7YRCM7GYHANOPUCBDUZP2H3A4MFWWI\",\"WARC-Block-Digest\":\"sha1:O5QAXVV5QCEEYA2M5HEXDHJOBH6DZLCY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487610196.46_warc_CC-MAIN-20210613161945-20210613191945-00433.warc.gz\"}"}