了解Common Lisp中的函数`tailp` [英] Understanding function `tailp` in Common Lisp

查看:95
本文介绍了了解Common Lisp中的函数`tailp`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看Bert Burgemeister的"Common Lisp快速参考"时,我偶然发现了tailp.

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))

但它回来了

NIL

CLTL2说,tailp是正确的 iff ,第一个参数是具有n的任何(nthcdr n list).

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)

我进一步尝试:

(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

直到我尝试为止(然后理解tailp寻找cdrcdr甚至共享同一地址):

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?)

备注:

好吧,慢慢地,我开始理解,也许这是列表的cdr部分的eq ...一种...给定列表cdr-衍生物>进入第一个参数?".

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?

在与@Lassi的漫长讨论中在这里,我们发现:

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

切勿在通函列表上使用tailp

Never Use tailp On Circular Lists!

因为行为是不确定的(在SBCL中已经出现问题). 因此tailp用于非圆形列表.

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

推荐答案

tailp的基本目的是检查是否存在共享的列表结构.这意味着 cons单元格是否相同(这意味着EQL作为谓词)-不仅仅是cons单元格的内容.

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.

还可以检查项目是否位于最后一个cdr中:

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

这是Common Lisp中很少使用的功能之一.

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

这篇关于了解Common Lisp中的函数`tailp`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆