Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding function `tailp` in Common Lisp

While looking through the "Common Lisp Quick Reference" by Bert Burgemeister, I stumbled over tailp.

First, I misunderstood the definitions of this function. And I tried:

(tailp '(3 4 5) '(1 2 3 4 5))

But it returned

NIL

CLTL2 says, tailp is true iff the first argument is any (nthcdr n list) with existing n.

(nthcdr 2 '(1 2 3 4 5))
;; (3 4 5)

I further tried:

(tailp '(3 4 5) '(1 2 3 4 5))
;; NIL - and I would expect: T following the definition above.

(tailp '() '(1 2 3 4 5))
;; T
(tailp '5  '(1 2 3 4 . 5))
;; T

Until I tried (and then understood tailp looks for cdr of l which share even the same address):

(defparameter l '(1 2 3 4 5 6))
(tailp (nthcdr 3 l) l)
;; T

But then I had my next question:

For what such a function is useful at all?

Wouldn't be a function more useful which looks whether a sublist is part of a list? (Or looks like a part of a list, instead that it has to share the same address?)

Remark:

Ah okay slowly I begin to understand, that maybe that this is kind of a eq for cdr parts of a list ... Kind of ... "Any cdr-derivative of given list eq to the first argument?".

But maybe someone can explain me in which situations such test is very useful?

Remark:

In a long discussion with @Lassi here, we found out:

Never Use tailp On Circular Lists!

Because the behavior is undefined (already in SBCL problematic). So tailp is for usage on non-circular lists.

like image 297
Gwang-Jin Kim Avatar asked May 26 '18 11:05

Gwang-Jin Kim


1 Answers

The basic purpose of tailp is to check whether there is list structure shared. This means whether the cons cells are the same (which means EQL as a predicate) - not just the content of the cons cells.

One can also check if an item is in the last cdr:

CL-USER 87 > (tailp t '(1 2 3 4 . t))
T

CL-USER 88 > (tailp nil '(1 2 3 4 . nil))
T

CL-USER 89 > (tailp nil '(1 2 3 4))
T

CL-USER 90 > (tailp #1="e" '(1 2 3 4 . #1#))
T

This is one of the rarely used functions in Common Lisp.

like image 105
Rainer Joswig Avatar answered Sep 28 '22 06:09

Rainer Joswig