Reputation: 6360
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.
So given a list: (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
.
Isn't (caddddr x)
the same as (car (cddddr x))
?
Upvotes: 0
Views: 891
Reputation: 35189
Because the scheme definition goes up to (cddddr pair)
but not beyond. In the words of the specification for car
and cdr
and friends: "Arbitrary compositions, up to four deep, are provided." 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.
Upvotes: 4
Reputation: 236034
You can only put a few a
's andd
'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. 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:
(define x '(1 2 3 4 5))
(list-ref x 4)
=> 5
Upvotes: 8