[Ecls-list] order problem

Marco Antoniotti marcoxa at cs.nyu.edu
Mon May 17 13:58:19 UTC 2004

On Monday, May 17, 2004, at 16:02 America/New_York, Michael O'Connor 

> I've got a problem with a piece of code that re-orders
> data which isn't working as I'd expect in ECL (but
> works as expected when run in CLisp), it can be
> illustrated with the following example:
> (setf data (list (list "a" 2) (list "b" 1)))
> (format t "Before: ~A~%" data)
> (sort data (lambda (a b) (< (second a) (second b))))
> (format t "After: ~A~%" data)
> In ECL the above produces:
> Before: ((a 2) (b 1))
> After: ((a 2))
> I'd expect the final line above to read 'After: ((b 1)
> (a 2))'. Am I doing something wrong?

SORT is destructive.  It changes the underlying representation of the 
list.  You cannot rely on it leaving DATA internal structure unchanged.
The fact that it works in CLISP is ok, but what you noticed in ECL is 
also OK as per the ANSI spec.
Try this instead.

(setf data (list (list "a" 2) (list "b" 1)))
(format t "Before: ~A~%" data)
(setf data (sort data  #'< :key #'second)) ; Note (1) the SETF and (2) 
the use of the :KEY in SORT.
(format t "After: ~A~%" data)


Marco Antoniotti					http://bioinformatics.nyu.edu
NYU Courant Bioinformatics Group		tel. +1 - 212 - 998 3488
715 Broadway 10th FL				fax. +1 - 212 - 998 3484
New York, NY, 10003, U.S.A.

More information about the ecl-devel mailing list