This is probably a simple thing I’m missing, but I’m trying to get the cdr of a pair and every call to say (cdr (cons 'a '5)) comes back as (5). I sort of get why that is, but how can I get the it to return without the parens?
I don’t want to use flatten because what I’m trying to get (i.e. the cdr) might itself be another procedure expression already wrapped in parens, so I don’t want to flatten the list.
(If it matters, I’m working on transforming a let expression into a lambda expression, and this is one of the steps I’m taking, trying to break apart the lambda bindings so I can move them around).
When applied to a proper list,
cdrwill always return another list (including'(), the empty list).With proper list I mean a list which ends with the empty list. For instance, when you do this
(define lst '(4 5))under the hood this is what gets assigned tolst:(cons 4 (cons 5 '())), so when you evaluate(cdr lst)you get the second element of the firstcons, which happens to be(cons 5 '()), which in turn gets printed as(5).For extracting only the second element in the list (not the second element of the first
cons, which is whatcdrdoes) you could:(car (cdr lst))or just(cadr lst)for short(second lst)(define cell (cons 4 5))or(define cell '(4 . 5))to build a cons cell and then you can use(car cell)to extract the first element and(cdr cell)to extract the second element.