Вопрос

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, cdr will 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 to lst: (cons 4 (cons 5 '())), so when you evaluate (cdr lst) you get the second element of the first cons, 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 what cdr does) you could:

  1. As has been pointed in the comments, use (car (cdr lst)) or just (cadr lst) for short
  2. Even simpler: use (second lst)
  3. Another possibility - if the list only has two elements and it's ok to replace it with an improper list, use (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.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top