[Ecls-list] order problem

Larry Clapp larry at theclapp.org
Mon May 17 14:22:13 UTC 2004


On Mon, May 17, 2004 at 01:02:42PM -0700, Michael O'Connor wrote:
> 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?

Yes.  SORT is destructive.

(setq data (sort data (lambda (a b) (< (second a) (second b)))))

> (setq a (list "a" 2))
("a" 2)
> (setq b (list "b" 1))
("b" 1)
> (setq c (cons a (cons b nil)))
(("a" 2) ("b" 1))
> (eq a (car c))
T
> (eq b (cadr c))
T
> (setq d (sort c (lambda (a b) (< (second a) (second b)))))
(("b" 1) ("a" 2))
> d
(("b" 1) ("a" 2))
> (eq b (car d))
T
> (eq a (cadr d))
T
> c
(("a" 2))
> (eq a (car c))
T
> (eq c (cdr d))
T

At the end of all this, C points to the same cons cell it did before,
but the SORT modified the contents of that cell.

Does this help?

-- Larry





More information about the ecl-devel mailing list