Skip to Content
Writing GNU Emacs Extensions
book

Writing GNU Emacs Extensions

by Bob Glickstein
April 1997
Intermediate to advanced
240 pages
5h 56m
English
O'Reilly Media, Inc.
Content preview from Writing GNU Emacs Extensions

Other Useful List Functions

Here are some more list-related Lisp functions that Emacs defines.

  • length returns the length of a list. It does not work on "improper" lists.

    (length nil) ⇒ 0
    (length '(x y z)) ⇒ 3
    (length '((x y z))) ⇒ 1
    (length '(a b . c)) ⇒ error
  • nthcdr calls cdr on a list n times.

    (nthcdr 2 '(a b c)) ⇒ (c)
  • nth returns the nth element of a list (where the first element is numbered zero). This is the same as the car of the nthcdr.

    (nth 2 '(a b c)) ⇒ c
    (nth 1 '((a b) (c d) (e f))) ⇒ (c d)
  • mapcar takes a function and a list as arguments. It calls the function once for each element of the list, passing that element as an argument to the function. The result of mapcar is a list of the results of calling the function on each element. So if you have a list of strings and want to capitalize each one, you could write:

    (mapcar '(lambda (x)
               (capitalize x))
            '("lisp" "is" "cool")) ⇒ ("Lisp" "Is" "Cool")
  • equal tests whether its two arguments are equal. This is a different kind of equality test from eq, which we first encountered in the section called Saving and Restoring Point in Chapter 3. Whereas eq tests whether its arguments are the same object, equal tests whether two objects have the same structure and contents.

    This distinction is important. In the following example:

    (setq x (list 1 2 3))
    (setq y (list 1 2 3))

    x and y are two different objects. That is, the first call to list creates a chain of three cons cells, and the second call to list creates a chain of three more cons cells. So ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GNU Emacs Pocket Reference

GNU Emacs Pocket Reference

Debra Cameron
Learning GNU Emacs, Second Edition

Learning GNU Emacs, Second Edition

Debra Cameron, Bill Rosenblatt, Eric S. Raymond

Publisher Resources

ISBN: 9781449395056Errata Page