Search code examples
common-lispsbclclispside-effects

Strange behaviour of nreverse in sbcl


When I enter

(defvar lst '(1 2 3 4 5))
(nreverse lst)
lst

in sbcl I get the answer (1), while in clisp I get the expected answer (5 4 3 2 1).

Interestingly enough the second line prints the reversed list, but the value stored in lst is wrong. Is this a feature or a bug? Should I use only reverse on sbcl?


Solution

  • nreverse may as a side-effect alter the original list in unspecified ways.

    You are supposed to only use the result of the function as the reversed list.

    As you can see from the side effect, SBCL and CLISP are using different implementation strategies. But you should not take advantage of that knowledge. Only use the function result. Use nreverse only, if efficiency is important and the original list can be destroyed.