LISP 汽车的最后一个元素?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
具有 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))))))
嗯,根据错误消息,没有
cadddar
函数。请记住,car
和cdr
是原始列表读取函数。其他诸如caddar
是由一个或多个car
和cdr
组合构建的便利函数。也就是说,如果caddar
等不存在,您可以仅使用car
和cdr
执行列表操作,扩展函数只是使你的生活更轻松一点。因此,解决此问题的方法是使用
car
和cdr
合成您自己的cadddar
。如果不太清楚如何执行此操作,请从更简单的开始(例如,使用cadr
或cdar
),然后逐步构建到cadddar
。Well, per the error message, there is no
cadddar
function. Keep in mind thatcar
andcdr
are the primitive list-reading functions. Others likecaddar
are convenience functions that are built from a combination of one or morecar
andcdr
. That is, you could perform list manipulation just fine with onlycar
andcdr
ifcaddar
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
usingcar
andcdr
. If it isn't immediately apparent how to do this, start simplier (with, say,cadr
orcdar
) and build up tocadddar
.标准没有定义超过 4 个
a
和d
的函数,可能是因为它们有 32 个 [并且从那时起它变得更加混乱] 。获取列表最后一个元素的可靠方法:
last
返回最后一个 cons 单元格,因此为您提供最后一个列表元素。当然,
list
也可以是其他内容,如(first list)
。The functions with more than 4
a
s andd
s 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, sogives you the last list element. Of course,
list
could be something else like(first list)
.