On Wed, Aug 31, 2005 at 11:12:48PM -0300, Fabricio Chalub wrote:
> It seems that either I am doing something wrong or SBCL's SORT is not
> working properly in the following code.
> (defparameter *vector* #(a b c))
> (defparameter *proper-list* '(a b c))
> (defparameter *d* (make-hash-table))
> (setf (gethash 'a *d*) 3)
> (setf (gethash 'b *d*) 2)
> (setf (gethash 'c *d*) 1)
> (defun cmp (x y)
> (< (gethash x *d*) (gethash y *d*)))
> (sort *proper-list* #'cmp)
This has to be:
(setf *proper-list* (sort *proper-list* #'cmp))
The CLHS page on SORT states:
The sorting operation can be destructive in all cases. In the case
of a vector 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.
CLHS on NREVERSE:
For nreverse, sequence might be destroyed and re-used to produce
the result. The result might or might not be identical to
sequence. Specifically, when sequence is a list, nreverse is
permitted to setf any part, car or cdr, of any cons that is part of
the list structure of sequence. When sequence is a vector, nreverse
is permitted to re-order the elements of sequence in order to
produce the resulting vector.
> (print *proper-list*) ; returns (A), should return (C B A)
> (sort *vector* #'cmp)
> (print *vector*) ; returns #(C B A), OK
> According to the hyperspec, if the predicate terminates, regardless of
> any meaningful interpretation of the ordering it provided, "no element
> will be lost" during a SORT.
> Strangely, ACL, CMUCL, and SBCL all share the same (erroneous?)
> behavior, whereas CLISP and LISPWORKS sort the list correctly.
> Any hints?
Max-Gerd Retzlaff <m.retzlaff@...>
For your amusement:
Nothing will dispel enthusiasm like a small admission fee.
-- Kim Hubbard