From: Jeff Clough on 27 Apr 2010 14:00 RG <rNOSPAMon(a)flownet.com> writes: > In article <83l6u0FmilU1(a)mid.individual.net>, > Pascal Costanza <pc(a)p-cos.net> wrote: >> On 26/04/2010 11:19, Captain Obvious wrote: >> > PC> Yes, the latter. The wording "can be" seems to be a defense of the >> > PC> design choice to make sort destructive. >> > >> > I think "can be" there merely means that if sequence is already sorted, >> > it just won't touch it. >> >> Nope. The spec clearly says that the result may not be identical to the >> first parameter. > > No, it says it *might* not be identical. Subtle but crucial difference. I believe the upshot of this is that the destructive or non-destructive nature of sort is implementation dependent. The spec does not force implementations to make the argument to sort useful after the call, so they are free to mangle it in any way they see fit. In other words, using the argument to sort after the call *may* work today, on your specific implementation, but it might not work tomorrow or on anyone else's implementation. Jeff
From: grucidipo on 28 Apr 2010 15:14 On 25 abr, 18:17, Tamas K Papp <tkp...(a)gmail.com> wrote: > Does SORT always sort vectors in place? The HS says > > "The sorting operation can be destructive in all cases. In the case of > a vector argument, this is accomplished by permuting the elements in > place. In the case of a list, the list is destructively reordered in > the same manner as for nreverse." > > It is unclear whether sorting vectors in place is an option ("can be > ...") or it is always done that way. To put it differently, if V is > known to be a vector, do I have to use > > (setf v (sort v predicate)) > > or can I just rely on > > (sort v predicate) > > to achieve the same effect? > > Thanks, > > Tamas I think we should define sort! or nsort in cases like this
From: Norbert_Paul on 29 Apr 2010 02:35 grucidipo wrote: > I think we should define sort! or nsort in cases like this I made a safe-sort which simply is x +-> (sort (copy-seq x)) (And after HE, the great FSM, hath enlighted me I even used it: http://groups.google.com/groups/search?q=comp.lang.lisp+"Is+LISP+divine%3F"&qt_s=Search+Groups )
From: Pascal J. Bourguignon on 29 Apr 2010 06:58 Norbert_Paul <norbertpauls_spambin(a)yahoo.com> writes: > grucidipo wrote: >> I think we should define sort! or nsort in cases like this > I made a safe-sort which simply is x +-> (sort (copy-seq x)) > > (And after HE, the great FSM, hath enlighted me I even used it: > http://groups.google.com/groups/search?q=comp.lang.lisp+"Is+LISP+divine%3F"&qt_s=Search+Groups > ) So we need two functions, one to sort functionnaly, and one to sort proceduraly: (defun safe-sort (sequence predicate &key key) (sort (copy-seq sequence) predicate :key key)) (defun sort-in-place (sequence predicate &key key) (let ((sorted (sort sequence predicate :key key))) (unless (eq sorted sequence) (replace sequence sorted)) sorted)) -- __Pascal Bourguignon__ http://www.informatimago.com
From: Norbert_Paul on 30 Apr 2010 05:00
Pascal J. Bourguignon wrote: > So we need two functions, one to sort functionnaly, and one to sort > proceduraly: Nice. > (defun safe-sort (sequence predicate&key key) > (sort (copy-seq sequence) predicate :key key)) I already have that. > (defun sort-in-place (sequence predicate&key key) > (let ((sorted (sort sequence predicate :key key))) > (unless (eq sorted sequence) > (replace sequence sorted)) > sorted)) I might use it later, when my code is running and I start to optimize. Norbert |