[英]Car and Cdr in Scheme
So I've been learn Scheme for school, and have run into a situation using car
and cdr
series that doesn't quite make sense to me. 因此,我一直在学校学习Scheme,并且遇到了对我来说不太有意义的使用
car
和cdr
系列的情况。
So given a list: (define x '(1 2 3 4 5))
因此给出一个列表:(
(define x '(1 2 3 4 5))
How come (caddddr x)
spits an error at me, while (cddddr x)
returns (5)
and (car (cddddr x))
returns 5
. (caddddr x)
对我吐出错误,而(cddddr x)
返回(5)
而(car (cddddr x))
返回5
。
Isn't (caddddr x)
the same as (car (cddddr x))
? (caddddr x)
是否与(car (cddddr x))
?
You can only put a few a
's and d
's in there :-) check the 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. 您只能在其中放几个
a
和d
:-)检查文档 ,起始c
和末尾r
之间可以有1-4个字符,而a
和d
的任意组合。 If you need to access a specific element beyond that, consider using list-ref
, which returns an element given its zero-based index on the list, for example: 如果您需要访问除此以外的特定元素,请考虑使用
list-ref
,它会返回给定元素在列表中从零开始的索引,例如:
(define x '(1 2 3 4 5))
(list-ref x 4)
=> 5
Because the scheme definition goes up to (cddddr pair)
but not beyond. 由于该方案定义上升到
(cddddr pair)
,但无法超越。 In the words of the specification for car
and cdr
and friends: "Arbitrary compositions, up to four deep, are provided." 用
car
, cdr
和朋友的规范的话来说:“提供了最多四层的任意组合。” See (for example): 参见(例如):
http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_idx_620
And as has been noted elsewhere, list-ref is probably what you want in this case. 正如其他地方所指出的,在这种情况下, list-ref可能就是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.