[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: MCL 2.0b1p3 sort problems

It is a common misperception that destructive functions in common lisp,
such as delete, nreverse, sort, etc. operate *soley* by side effect.
This is not true.  As noted in CLtL when describing NREVERSE:

  ``The result may or may not be EQ to the argument, so it is
    usually wise to say something like (SETQ X (NREVERSE X)),
    because simply (NREVERSE X) is not guarenteed to leave a
    reversed value in X.''

This applies to all destructive operations, use (SETQ X (SORT X #'<)), etc.

This misperception is probably due to the fact that relying on side
effect will work *sometimes*.  The confusion and irate messages
about Common Lisp are due to the fact that relying on side effect
will not always work.

A typical example is deleting an element from a list.  If it turns
out that you are keeping the first element, then side effect alone
will work fine:

    (setq x '(1 2 3 4 5 6))

    (delete-if #'evenp x) => (1 3 5)

    x => (1 3 5)

However, if you are deleting the first element then you will get a

    (setq x '(1 2 3 4 5 6))

    (delete-if #'oddp x) => (2 4 6)

    x => (1 2 4 6)