2

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]
4

4 回答 4

9

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

于 2010-11-19T04:57:35.300 回答
2

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.

于 2010-11-19T04:56:58.593 回答
1

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

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

(car (last list))

给你最后一个列表元素。当然,list也可以是其他类似的东西(first list)

于 2012-03-26T10:57:43.453 回答
1
(first (last (first '(((1 2) 3 4 5) ((6 7) 8 9 10)))))

-> 5
于 2010-11-19T08:35:25.193 回答