Re: the sort function in lisp (destructive)



In article <4780e8b3$0$90268$14726298@xxxxxxxxxxxxxxx>,
"Alex Mizrahi" <udodenko@xxxxxxxxxxxxxxxxxxxxx> wrote:

i agree that it would be more newbie-friendly to have either non-destructive
sort by default or sortf,
but Common Lisp is not a newbie-oriented language, and more experienced Lisp
programmers use SORT without problems (or they can easily define function or
macro that fits them better).

Another alternative would be a SORT that does not reorder the CONS cells,
but the CARs of the CONS cells. SORT the list into another
data structure and then set the list's CARs from there.
Though even that would not be that useful.


(defun my-sort (list predicate)
(map-into list
#'identity
(sort (copy-list list) predicate)))


(let ((l1 (list 'd 'e 'a 'f 'x 'h)))
(let ((l2 (sort l1 #'string<)))
(values l1 l2)))

->
(D E F H X)
(A D E F H X)

(let ((l1 (list 'd 'e 'a 'f 'x 'h)))
(let ((l2 (my-sort l1 #'string<)))
(values l1 l2)))

->
(A D E F H X)
(A D E F H X)



if newbies will spend some hours debugging weird SORT behaviour it would be
better for them -- that way they'll understand understand faster what does
word "destructive" mean and how variables do work.

--
http://lispm.dyndns.org/
.



Relevant Pages

  • Re: Newbie style questions
    ... It is also, in Common Lisp, also a subtly bad example. ... implementation of the SORT algorithm. ... It varies with the task and the preferences of the programmer. ... which is why some people prefer iteration. ...
    (comp.lang.lisp)
  • Re: Idiomatic lisp - loops
    ... Is a program that needs real speed on large data sets really using dotted pairs in a list as the data structure? ... If what he really wanted was a tutorial on how to write clever schemisms in common lisp, then, hey, go to town with labels and the rest. ... It provides a means of adding an element to an existing list and a means to stably sort an existing list destructively, which takes not one, but two functional arguments for customization. ...
    (comp.lang.lisp)
  • CL Implementations and Tail-Call Elimination
    ... where they use recursion to do iteration, and usually one of the first ... can't rely on an ANS Common Lisp implementation to eliminate tail ... So I'm sort of wondering which common Common Lisps don't do that. ... this is a mildly bad habit or a really bad habit, ...
    (comp.lang.lisp)
  • Re: Newbie style questions
    ... If you want to sort short lists, then other sort algorithms will be ... > Does the Common Lisp standard require tail call optimizations? ... but most implementations have it. ...
    (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)