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.
EQL
is the default.TAILP
doesn't seem like an especially useful test. A quick grep of my installed quicklisp dists shows cl-yacc as the only user. From a quick glance over the code, it seems like it gets two cons-cells (OP1-TAIL
andOP2-TAIL
) from the same list (the grammar precedence), and usesTAILP
to check ifOP2-TAIL
is a tail of the cdr ofOP1-TAIL
, which would mean thatOP1
came first in the precedence list. – Khivasame
==EQL
in the lisp explanations and definitions. Okay ...TAILP
could then serve as a precedence/successor test within lists ... since it looks directly into he loci, it might then be more performant than other constructed tests, isn't it? – Arc