Re: randomize-list



(message (Hello 'Matthias)
(you :wrote :on '(28 May 2007 02:26:48 -0700))
(

MB> Hmm... How about:

MB> (defun sort-list-shuffle (list)
MB> (sort list #'< :key (lambda (x) (declare (ignore x)) (random 1.0))))

MB> Would that work as well?

although some (naive, or over-optimized) implementations of sort can either
duplicate/omit elements, or hang, it apears that standard requires sort to
return something even if predicate or key is weird:
---
If the key and predicate always return, then the sorting operation will
always terminate, producing a sequence containing the same elements as
sequence (that is, the result is a permutation of sequence). This is
guaranteed even if the predicate does not really consistently represent a
total order (in which case the elements will be scrambled in some
unpredictable way, but no element will be lost).
---
and does not require predicate or key to be consistent:

---
If the key consistently returns meaningful keys,
---

i think "If" here implicitly allows key to return inconsistent and
non-meaningful keys :)

so i think this should work, although i wouldn't bet that all
implementations handle this correctly :)

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"I am everything you want and I am everything you need")


.



Relevant Pages