LISP 汽车的最后一个元素?

发布于 2024-10-03 03:16:07 字数 529 浏览 8 评论 0原文

LISP 再次难倒了我...为什么我无法获取列表中最后一个元素的值?我有一个与此类似的列表设置:

(setq bar '(((1 2) 3 4 5)((6 7) 8 9 10)))

现在我得到 4 的回报:

(caddar bar)

有 (5) 的回报:

(cdddar bar)

但我无法得到 5 的:

(cadddar bar)

这是为什么——以及如何获得5的值?

错误:

; Warning: This function is undefined:
;   CADDDAR

Error in KERNEL:%COERCE-TO-FUNCTION:  the function CADDDAR is undefined.
[Condition of type UNDEFINED-FUNCTION]

LISP stumps me yet again... Why can't I get the value of the last element in a list? I have a list set up similar to this:

(setq bar '(((1 2) 3 4 5)((6 7) 8 9 10)))

Now I get a return of 4 for:

(caddar bar)

There is a return of (5) for:

(cdddar bar)

But I can't get a 5 for:

(cadddar bar)

Why is this--and how do I get the value of the 5?

Error:

; Warning: This function is undefined:
;   CADDDAR

Error in KERNEL:%COERCE-TO-FUNCTION:  the function CADDDAR is undefined.
[Condition of type UNDEFINED-FUNCTION]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别靠近我心 2024-10-10 03:16:08

具有 5 个或更多 a 和 d 的函数未定义。只有 4 个或更少。这么长的函数可能有太多,不切实际。

你必须把它拼出来:
(car (cdr (cdr (cdr (cdr (car x))))))

The functions with 5 or more a's and d's are not defined. Only 4 and fewer. There are too many possible functions of that length for it be be practical.

You have to just spell it out:
(car (cdr (cdr (cdr (cdr (car x))))))

铜锣湾横着走 2024-10-10 03:16:08

嗯,根据错误消息,没有 cadddar 函数。请记住,carcdr 是原始列表读取函数。其他诸如 caddar 是由一个或多个 carcdr 组合构建的便利函数。也就是说,如果 caddar 等不存在,您可以仅使用 carcdr 执行列表操作,扩展函数只是使你的生活更轻松一点。

因此,解决此问题的方法是使用 carcdr 合成您自己的 cadddar。如果不太清楚如何执行此操作,请从更简单的开始(例如,使用 cadrcdar),然后逐步构建到 cadddar

Well, per the error message, there is no cadddar function. Keep in mind that car and cdr are the primitive list-reading functions. Others like caddar are convenience functions that are built from a combination of one or more car and cdr. That is, you could perform list manipulation just fine with only car and cdr if caddar etc. didn't exist, the extended functions just make your life a bit easier.

So, the way to approach this is to synthesize your own cadddar using car and cdr. If it isn't immediately apparent how to do this, start simplier (with, say, cadr or cdar) and build up to cadddar.

北城半夏 2024-10-10 03:16:08

标准没有定义超过 4 个 ad 的函数,可能是因为它们有 32 个 [并且从那时起它变得更加混乱] 。

获取列表最后一个元素的可靠方法:last 返回最后一个 cons 单元格,因此为

(car (last list))

您提供最后一个列表元素。当然,list 也可以是其他内容,如 (first list)

The functions with more than 4 as and ds aren't defined by the standard, maybe because there are 32 of them [and it gets exponentially messier from then on].

A sure-fire way to get the last element of a list: last returns the last cons cell, so

(car (last list))

gives you the last list element. Of course, list could be something else like (first list).

云之铃。 2024-10-10 03:16:08
(first (last (first '(((1 2) 3 4 5) ((6 7) 8 9 10)))))

-> 5
(first (last (first '(((1 2) 3 4 5) ((6 7) 8 9 10)))))

-> 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文