Re: the sort function in lisp (destructive)



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


.



Relevant Pages

  • Re: the sort function in lisp (destructive)
    ... but the CARs of the CONS cells. ... SORT the list into another ... data structure and then set the list's CARs from there. ...
    (comp.lang.lisp)
  • Re: the sort function in lisp (destructive)
    ... As for SORT Robert Maas posted a small hack that would preserve the head ... cons after calling the implementation's SORT in SORT-KEEPING-HEAD-CELL ... then splice the user's head cell back in after sorting. ...
    (comp.lang.lisp)
  • Re: No Skipweaselling Please - Were Welsh
    ... The other aspect that drivers don't give enough weight to is the erratic ... Insurance companies don't penalise powerful cars for nothing - they know ... - that sort of thing. ... There would of course be gun owners who thought ...
    (uk.rec.sheds)
  • Re: ANSI Common Lisp, Ch 3, Ex 3
    ... Others have pointed out that SORT takes an optional ... (let ((node nil) ... (setq remainder (occurences-remainder node sorted-list)) ... to the end of the list or using CONS on the front, ...
    (comp.lang.lisp)
  • Re: OT - BBC TV - Ping my British friends!
    ... Mike Brown wrote: ... obsolete ideas - such as the more cars, and the bigger they are, the ... I read all this carefully and he seems like an OK sort of bloke to me. ... You know there are things about which one does not joke. ...
    (rec.music.makers.guitar.acoustic)