0

In this example, there is a cons-cell (foo . bar) which needs a two (2) step approach so that it can be copied using copy-sequence:

  • First, there needs to be a test to identify this particular type of cons-cell (foo . bar) so that the transformation function(s) are not unnecessarily applied to lists that are of a different form.

  • Second, once this particular type of cons-cell is identified, the cons-cell needs to be transformed into a list that copy-sequence will accept.

How can this be accomplished?

2
  • Using your test in the first comment, and then creating the new list in your second comment, resolves the issue -- thank you very much! I will also play with proper-list-p, which I haven't tried before. Greatly appreciated. I'm operating a sort function, which is destructive and I'd like to keep a copy of the original list for later use.
    – lawlist
    Feb 4, 2022 at 3:52
  • Comments now converted to an answer.
    – phils
    Feb 4, 2022 at 12:05

1 Answer 1

2

You have your test right there in the error message:

(copy-sequence (cons 'foo 'bar))
=> Lisp error: (wrong-type-argument listp bar)

I.e. for a cons cell CONS the test is:

(listp (cdr CONS))

You can also use (proper-list-p CONS) as a test, if you want to detect improper lists in general; but your example is more specific than this.

When your test fails, you could create a list from its components with:

(list (car CONS) (cdr CONS))

There's no point to calling copy-sequence on that, though -- you've already generated a new structure containing the values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.