Tracks
/
Common Lisp
Common Lisp
/
Exercises
/
Leslie's Lengthy Lists
Leslie's Lengthy Lists

Leslie's Lengthy Lists

Learning Exercise

Introduction

Lists

Given that the name of the language we're using, Lisp, stands for LIST Processing, one might assume that the language has facilities for handling lists of items, and you'd be correct!

While Common Lisp has data structures other than lists, lists are still heavily used.

A list in Common Lisp is a sequence of items. It's also called a cons (for historical reasons, mostly). The items themselves do not have to be the same type. For example you can have a list of 1, two, "III".

Creating Lists

You can simply type in a quoted list like this: '(1 two "III") and that will cause a list to be created and evaluated (it evaluates to (1 two "III")).

There are also two main functions used to create lists: list and cons.

list takes zero or more arguments and evaluates to a list created with those values:

(list 1 'two "III") ; => (1 two "III")

cons takes two items and creates a list which has the first item as its car (the list head) and the second item as its cdr (the rest of the list):

(cons 1 2)            ; => (1 . 2) ;; (a list without `nil` as its `cdr` is printed in this way)
(cons 1 nil)          ; => (1)
(cons 1 (cons 2 nil)) ; => (1 2)

The functions car and cdr can be used to access the car and cdr of a list.

(first and rest are synonyms of car and cdr and work exactly the same.)

Length & Random Access

The length of a list can be determined by the use of length. An empty list has length zero.

An arbitrary item can be accessed with nth (note that lists are zero-indexed).

It is not an error to request an index that is bigger than the list is long. Instead, it evaluates to nil:

(nth 23 '(short list))` ; => nil

There are also several helper methods for accessing the first items of a list: first, second, third, all the way until tenth. Nifty, huh?

Combining lists

Two or more lists can be combined with append:

(append '(a b c) '(1 2 3)) ; => (A B C 1 2 3)
(append '(a b c) '()))     ; => (A B C)

Each argument given to append needs to be a list.

Instructions

Leslie the Lisp Alien needs to do some shopping. It is very important to have a shopping list. One needs to add things to it, and remove things from it.

Of course simple pen and paper will not do for a Lisp Alien. "List" is most of the word "Lisp" even!. There must be some functions written to help keep track of the shopping.

Can you help Leslie keep track of the shopping list?

1. Making a new list

First thing is that Leslie needs to create a empty list. A function called new-list would be perfect for that.

(new-list) ; => ()

Oh no... Leslie has a few things in mind already, so they need a function that takes three items (luckily Leslie only creates a list of three items. Nothing more, nothing less!) and creates a new shopping list with those things. Write a function, list-of-things which takes three items and returns a list of them.

(list-of-things 'bread 'milk 'butter) ; => '(bread milk butter)

2. Add things to the list.

Before going to the store Leslie looks in the pantry to see what they need. Help them by writing the function add-to-list which adds an item to the beginning of a list.

(add-to-list 'butter '(bread)) ; => '(butter bread)

3. What's next thing(s) on the list?

When shopping, Leslie wants to know what to look for next. They also like to peek ahead at the list to see the second, third, or even the 23rd item (their lucky number).

  • first-thing will evaluate to the first thing on the list
  • second-thing will evaluate to the second thing
  • third-thing will evaluate to the third thing
  • twenty-third-thing will evaluate to the twenty-third thing
(first-thing '(bread butter milk)) ; => 'bread
(second-thing '(bread butter milk)) ; => 'butter
(third-thing '(bread butter milk)) ; => 'milk

4. Removing a thing from the list

Leslie wants to find the first item of the list and remove the it from the shelf. Help them out by writing a function remove-first-item which evaluates to a list with everything but the first item on the input list.

(remove-first-item '(bread butter milk)) ; => '(butter milk)

5. Bigger lists out of smaller lists

Leslie realized they accidentally made two shopping lists, not one! Write a function, list-append which returns a list that with the elements from the first list followed by the ones from the second list.

(list-append '(bread salt) '(butter milk)) ; => '(bread salt butter milk)

6. How much longer?

Leslie is getting worried that this shopping trip will take a while. Just how many things are on this list? Write a function just-how-long to tell them how long their list is.

(just-how-long '(bread milk butter salt)) ; => 4
Edit via GitHub The link opens in a new window or tab
Common Lisp Exercism

Ready to start Leslie's Lengthy Lists?

Sign up to Exercism to learn and master Common Lisp with 27 concepts, 90 exercises, and real human mentoring, all for free.