Scheme中的Car和Cdr

Car and Cdr in Scheme

所以我一直在学习 Scheme for school,并且 运行 遇到了使用 carcdr 系列的情况,这对我来说不太有意义。

所以给出一个列表:(define x '(1 2 3 4 5))

为什么 (caddddr x) 向我吐出一个错误,而 (cddddr x) returns (5)(car (cddddr x)) returns 5 .

(caddddr x)(car (cddddr x))不一样吗?

因为方案定义达到 (cddddr pair)‌‌ 但不会超出。用 carcdr 和朋友的规范的话来说: "Arbitrary compositions, up to four deep, are provided." 参见(例如):

http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_idx_620

正如其他地方所指出的,list-ref 可能是您在这种情况下想要的。

你只能放几个 ad :-) 检查 documentation, between the initial c and the final r there can be between 1 and 4 characters in any combination of a's and d's. If you need to access a specific element beyond that, consider using list-ref,其中 returns 一个元素给定它的零- 基于列表的索引,例如:

(define x '(1 2 3 4 5))
(list-ref x 4)
=> 5