Skip to main content

Chapter 7: Sequences and Iteration

  • Chapter
Common Lisp Recipes
  • 2032 Accesses

Abstract

Lists (see Chapter 2) and vectors (see Chapter 5) have in common that you can traverse them sequentially. That’s why they are subsumed under the type SEQUENCE in Common Lisp. There are a lot of functions in the standard that work on sequences, which is to say that they accept lists as well as vectors as arguments. (And also strings, see Chapter 3, because those are vectors, too.) We’ll discuss most of them in this chapter. (And see also Recipe 3-7.)

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

Access this chapter

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

Institutional subscriptions

Notes

  1. 1.

    If not, Practical Common Lisp has a good, if somewhat brief, introduction to the LOOP macro in its Chapter 22. See http://www.gigamonkeys.com/book/loop-for-black-belts.html .

  2. 2.

    The family also has some black sheep the names of which start with “DELETE.” You should avoid them unless you really need them (see page 534).

  3. 3.

    One notable exception is COUNT.

  4. 4.

    Consider what that would mean for lists (see Recipe 2-1).

  5. 5.

    See http://metamodular.com/reverse-order.pdf .

  6. 6.

    Recipe 1-9 explains why we can use STRING= to compare symbols.

  7. 7.

    That’s the technical wording. If your predicate is a strict total order, like for example <, then this simply means that the two elements are equal.

  8. 8.

    See Recipe 17-6.

  9. 9.

    See Recipe 1-3.

  10. 10.

    Depending on the length of the cycle the second variant may also be a bit less efficient because of the usage of NTH.

  11. 11.

    LOOP also understands the equivalent loop keywords UPFROM and UPTO, but they are rarely used because the default is to count upwards anyway.

  12. 12.

    In our specific example it could have known (because 1 and 5 are constant values), but understandably LOOP doesn’t make any special provisions for such “trivial” cases.

  13. 13.

    This is based on CLISP’s LOOP. You might see something completely different in your Lisp, but the essence will be the same.

    I’ve taken the liberty to mark internal variables (which in the original macro expansion are uninterned symbols) with percent signs so that they stand out.

  14. 14.

    And (+ I 1) will become (- I 1).

  15. 15.

    See Recipe 2-1 for ATOM.

  16. 16.

    For real code, you probably want to check that you still have enough of DATA left before calling SUBSEQ.

  17. 17.

    See also Recipe 10-4.

  18. 18.

    And the same for DOLIST.

  19. 19.

    And the same holds for all sequence functions that can work on more than one sequence (see for example Recipe 7-12).

  20. 20.

    Related to what you would do with break in a for loop in C or Java.

  21. 21.

    Note that the AND is necessary. Without it, the counter would be incremented for each character and not only for each vowel.

  22. 22.

    The other one is WHILE and in a way you can also add THEREIS, ALWAYS, and NEVER.

  23. 23.

    Actually a function designator, see Recipe 8-5.

  24. 24.

    See Recipe 7-8.

  25. 25.

    OK, MAPCAR and MAPLIST make kind of sense, but the others…

  26. 26.

    Not generic in the CLOS sense, mind you.

  27. 27.

    And if this first argument is NIL, MAP works only for the side effects like MAPC.

  28. 28.

    In other words, the container will be destructively modified.

  29. 29.

    If you need something like that, you might want to have a look at Recipes 7-14 and 14-4 for a more realistic approach.

  30. 30.

    And, at least in theory, additional sequence types offered by the implementation.

  31. 31.

    Available online at http://www.doc.gold.ac.uk/~mas01cr/papers/ilc2007/sequences-20070301.pdf .

  32. 32.

    We implemented just two of them and we could only get away with that because we refrained from using certain sequence functions for our demonstration.

  33. 33.

    In case you’re thinking that the comparison is biased toward iterate: I’m always using LOOP for my daily work for the reasons described in the introduction to this chapter. But I nevertheless think that iterate is superior to LOOP in almost every area. Too bad it’s not in the standard…

  34. 34.

    Imagine, instead of PRINT, an expensive computation or something that’ll signal an error if I is too large. Or simply something with a side effect like PRINT itself.

  35. 35.

    See also https://common-lisp.net/project/iterate/doc/Differences-Between-Iterate-and-Loop.html . For a very detailed comparison see Sabra Crolleton’s article at https://sites.google.com/site/sabraonthehill/loop-v-iter .

  36. 36.

    If you’re into historical studies, you can have a look at it at https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/lisp/code/iter/loop/mit/0.html .

  37. 37.

    Or look here: http://users.actrix.co.nz/mycroft/db/ .

  38. 38.

    See Chapter 5 for VECTOR-PUSH-EXTEND, fill pointers, adjustable arrays, and so on.

  39. 39.

    In the usual sense of the word, not in the sense of Common Lisp’s sequences.

  40. 40.

    See Recipe 7-1.

  41. 41.

    That’s why you shouldn’t evaluate the form (ALL-PRIMES) in your REPL! (Unless you have set *PRINT-LENGTH*—see Recipe 9-1—to some sensible value.)

Author information

Authors and Affiliations

Authors

Electronic Supplementary Material

Below is the link to the electronic supplementary material.

chapter-07 (zip 4 kb)

Rights and permissions

Reprints and permissions

Copyright information

© 2016 Edmund Weitz

About this chapter

Cite this chapter

Weitz, E. (2016). Chapter 7: Sequences and Iteration. In: Common Lisp Recipes. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-1176-2_7

Download citation

Publish with us

Policies and ethics