Skip to main content

Chapter 2: Conses, Lists, and Trees

  • Chapter
  • 2041 Accesses

Abstract

While you also have hash tables (see Chapter 6) and arrays (see Chapter 5), conses and lists remain the most important building block for data structures in Common Lisp, if only because that’s how the internal representation of your code is realized and you’ll thus need them if you want to write macros and domain-specific languages. Also, because Common Lisp has such a plethora of built-in convenience functions to manipulate conses and lists, and because conses are at the same time very simple yet extremely versatile, there are lots of situations where you should just use them (whereas if you’re coming from another programming language, your first instinct might tell you to use, say, arrays). Some of the recipes in this chapter will thus show how widely-used data structures can be implemented on the basis of cons cells.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   79.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   99.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Notes

  1. 1.

    Online at http://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list-processing.html and http://www.gigamonkeys.com/book/beyond-lists-other-uses-for-cons-cells.html .

  2. 2.

    Another good source for how lists work is the book Common Lisp by David S. Touretzky (Dover Publications, 2013), which is available online at https://www.cs.cmu.edu/~dst/LispBook/ .

  3. 3.

    That’s an abbreviation for constructing; in the Lisp world, it is used as a noun like above but also as a verb (see Recipe 17-6).

  4. 4.

    See https://en.wikipedia.org/wiki/Ordered_pair .

  5. 5.

    See Recipe 16-11 for the meaning of *and **.

  6. 6.

    And like with cons, these two are also used as nouns: “The car of (4 . 2) is 4.”

  7. 7.

    And in case you’re wondering: there’s no way to confuse the dot in the middle with a symbol the name of which is only a dot. A stand-alone dot is only allowed as a separator in conses, and if you want to use it as a symbol name, it has to be escaped (see Recipe 8-8 and section 2.3.3 of the standard).

  8. 8.

    Interestingly, and although you can customize almost everything else in Common Lisp, there’s no portable way to convince the Lisp printer to use the dot notation for lists. But there’s a nice little library called draw-cons-tree that can be used to draw ASCII pictures of conses similar to the pictures in this chapter.

  9. 9.

    You can also imagine them as pointers except that there’s no pointer indirection; the car of this cons is the number 42 and not a pointer to it.

  10. 10.

    I will generally lay out conses so that they somehow “look nice,” which is to say that it, of course, doesn’t matter whether an arrow points down, or to the right, or somewhere else.

  11. 11.

    And it is not too far-fetched in this context to think of NIL as the NULL pointer in C.

  12. 12.

    This is the Landau notation typically used in computation complexity theory (see https://en.wikipedia.org/wiki/Big_O_notation ).

  13. 13.

    See Recipe 12-1 for CHECK-TYPE.

  14. 14.

    There’s also a corresponding function NULL(notNULLP”). NULL does exactly the same as NOT. Which of the two you use should depend on which intention you want to convey to the reader of your code.

  15. 15.

    With the slight difference that this version returns the empty list if X is zero—which is more consistent anyway.

    For the use of NREVERSE see the footnote on page 60.

  16. 16.

    Although what you’ll get isn’t a list in a narrower sense but rather an alist, a plist, or a set. (See Chapter 6.)

  17. 17.

    See https://en.wikipedia.org/wiki/Transpose .

  18. 18.

    We used the pretty printer to make the result look good (see Recipe 9-9).

  19. 19.

    Which implies that the form must evaluate to a list.

  20. 20.

    Like, for example, JavaScript’s push in addition to unshift.

  21. 21.

    Note that *LIST* will now have the value you wanted it to have, but the original list wasn’t changed. See more below.

  22. 22.

    By the way, this is one of the few situations where the use of a destructive operation (see page 544) like NREVERSE instead of REVERSE would not be dangerous at all; you have created the list yourself, the original list (the one in the wrong order) will be thrown away anyway, and nobody else holds a reference to it or some tail of it. Still, for a list as short as this one, it is probably premature optimization (see the introduction to Chapter 17).

  23. 23.

    LAST gives you the last cons (or the last n conses if used with an optional argument) of a list. Of course, it still has to traverse the list in order to achieve that.

  24. 24.

    See https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice .

  25. 25.

    Unless you don’t care whether the :NEW argument is destructively modified.

  26. 26.

    NTHCDR does what its name says: it finds the nth cdr of a list. Like any other list function it has to traverse the list to do so.

  27. 27.

    See page 544.

  28. 28.

    See step ((ii)) above.

  29. 29.

    Note that this happened although we used APPEND. While APPEND—in contrast to NCONC—is required to construct a new list structure, it will reuse its last argument.

  30. 30.

    Note that the order of the arguments has to be reversed!

  31. 31.

    This is about the abstract data structure called tree. If you were thinking of plants, you bought the wrong book.

  32. 32.

    For the setf functions, see Recipe 10-8.

  33. 33.

    Which is not to say that you shouldn’t represent tree nodes as CLOS objects if you think it’ll fit your problem domain better. But this is a chapter about conses and what you can do with them.

  34. 34.

    See Recipe 18-2.

  35. 35.

    See https://en.wikipedia.org/wiki/Queue_%28abstract_data_type%29 .

  36. 36.

    Minus the &ENVIRONMENT parameter, but including a &WHOLE parameter.

  37. 37.

    http://www.cliki.net/pattern%20matching provides a (probably incomplete) list of them.

  38. 38.

    Note the special quasiquote notation (see Recipe 2-4) used here.

Author information

Authors and Affiliations

Authors

Electronic Supplementary Material

Below is the link to the electronic supplementary material.

chapter-02 (zip 3 kb)

Rights and permissions

Reprints and permissions

Copyright information

© 2016 Edmund Weitz

About this chapter

Cite this chapter

Weitz, E. (2016). Chapter 2: Conses, Lists, and Trees. In: Common Lisp Recipes. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-1176-2_2

Download citation

Publish with us

Policies and ethics