Reputation: 21
Some answers on here just say that you can chain multiple car and cdr to reduce the clutter they create so, you could use (cadr (list)) instead of (car (cdr (list) )), but I have tried chaining multiple car and cdr and sometimes it will stop working on like the 5th one or something, I tried googling if there is a limit or something but couldnt find any answers.
specifically my problem shows here -> trying to get 7 from a list only using car and cdr.
list is (1(2(3(4(5(6(7)))))))))
(display (car(car(cdr(car(cdr(car(cdr(car(cdr(car (cdr (car (cdr '(1(2(3(4(5(6(7)))))))))))))))))))))
-> shows 7
(display (caadadadadadadr '(1(2(3(4(5(6(7)))))))))
-> shows error
Is this not how this works?
Upvotes: 2
Views: 192
Reputation: 91857
You aren't specific about which Scheme you're using. But for every Scheme implementation I'm aware of, there's a limit to how many car/cdr compositions are provided. In R5RS, for example, we see
library procedure: cddddr pair
These procedures are compositions of
car
andcdr
, where for examplecaddr
could be defined by(define caddr (lambda (x) (car (cdr (cdr x))))).
Arbitrary compositions, up to four deep, are provided. There are twenty-eight of these procedures in all.
Upvotes: 1