You are previewing Land of Lisp.

Land of Lisp

Cover of Land of Lisp by Conrad Barski M.D. Published by No Starch Press
O'Reilly logo

More Complex Macros

Let’s suppose you need a custom my-length command. This is a classic list-eating function that will count the length of a list. We’ll write it in the proper “tail call optimized” style (discussed in Chapter 14), where the recursive function call is in the tail position. Here’s the code:

(defun my-length (lst)
   (labels ((f (lst acc)
              (if lst
                (f (cdr lst) (1+ acc))
      (f lst 0)))

As you can see, this function has tons of repetitive ...

The best content for your career. Discover unlimited learning on demand for around $1/day.