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

Re: MCL 2.0b1p3 sort problems

   Date: Wed, 10 Jul 91 02:33:31 EDT
   From: Adnan Hamid <ahamid@clarity.Princeton.EDU>
   To: info-mcl@cambridge.apple.com
   Subject: MCL 2.0b1p3 sort problems
   According to my copy of Steel, (sort <sequence> <predicate>) *distructively* 
   sorts the sequence that it is given.  One presumes that the following behaviour
   is incorrect:
   Welcome to Macintosh Common Lisp Version 2.0b1p3!
   ? (setq foo '(1 2 3))
   (1 2 3)
   ? (sort foo #'>)
   (3 2 1)
   ? foo
CLtL Second Edition p. 408 states:

"The sorting operation may be destructive in all cases.  In the case
of an array argument, this is accomplished by permuting the elements
in place.  In the case of a list, the list is destructively reordered
in the same manner as for NREVERSE. Thus if the argument should not be
destroyed, the user myst sort a copy of the argument."

The SORT in MCL 1.3.2 preserved the initial cons when sorting so that
it still pointed at the sorted list (the rest of whose conses were
completely reordered).  It did this at the expense of consing, however.

If you like this behavior, you can easily do it yourself:

(defun consing-sort (sequence predicate &key key)
 (if (and sequence (listp sequence))
   (let ((first-cons sequence)
         (res (sort (cons (car sequence) (cdr sequence)) predicate
                    :key key)))
     (setf (car first-cons) (car res)
           (cdr first-cons) (cdr res))
   (sort sequence predicate :key key)))