Re: the sort function in lisp (destructive)
- From: "Alex Mizrahi" <udodenko@xxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 6 Jan 2008 19:15:12 +0200
RJ> Another alternative would be a SORT that does not reorder the CONS
RJ> cells, but the CARs of the CONS cells. SORT the list into another
RJ> data structure and then set the list's CARs from there.
and if implementation uses bubble-sort algorithm it doesn't even need any
additional storage!
but i think we can confuse Xah without introducing any significant
runtime/space overhead:
(defun pseudo-nice-sort (list pred)
(when list
(let ((beheaded-list (cons (first list) (rest list))))
(setf beheaded-list (sort beheaded-list pred))
(setf (car list) (car beheaded-list)
(cdr list) (cdr beheaded-list))
list)))
it will not "destroy variable" because it preserves the first CONS:
CL-USER> (let ((l1 (list 'd 'e 'a 'f 'x 'h)))
(let ((l2 (pseudo-nice-sort l1 #'string<)))
(values l1 l2)))
(A D E F H X)
(A D E F H X)
however it's still destructive, so in some cases it can bring more than a
few hours of happy debugging
.
- References:
- Re: the sort function in lisp (destructive)
- From: Alex Mizrahi
- Re: the sort function in lisp (destructive)
- From: Rainer Joswig
- Re: the sort function in lisp (destructive)
- Prev by Date: Re: the sort function in lisp (destructive)
- Next by Date: Re: dolist style
- Previous by thread: Re: the sort function in lisp (destructive)
- Next by thread: Re: the sort function in lisp (destructive)
- Index(es):
Relevant Pages
|