[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: randomize-list

William James

11/15/2015 9:02:00 PM

Pascal Bourguignon wrote:

> > (defun remove-nth (a n)
> > (declare (list a) (fixnum n))
> > (loop for item in (remove nil
> > (loop for i of-type fixnum from 0 below (length a) collect
> > (if (/= i n)
> > (cons (nth i a) nil))))
> > collect (car item)))
>
> (defun remove-nth (index list)
> (remove-if (constantly t) list :start index :end (1+ index)))

MatzLisp (Ruby):

a =%w(a b c d)
==>["a", "b", "c", "d"]
a.slice! 2
==>"c"
a
==>["a", "b", "d"]


> > (defun randomize-list (lst)
> > (let (l n (len (length lst)))
> > (loop while (> len 0) do
> > (setf n (random len))
> > (push (nth n lst) l) ; copy element
> > ; copy all from list a to list b but nth 'n element
> > (setf lst (remove-nth lst n))
> > (decf len))
> > l))
>
> (defun randomize-sequence (sequence)
> (loop
> :with vector = (coerce sequence 'vector)
> :for i :from (1- (length vector)) :downto 1
> :do (rotatef (aref vector i) (aref vector (random i)))
> :finally (return (coerce vector (type-of sequence)))))

%w(a b c d e f g h).shuffle
==>["d", "e", "g", "c", "a", "h", "b", "f"]

--
The Ortagard school in Rosengard, an area of Malmo with close to 100% Muslim
immigrants, is burning yet again. Several police patrols are called out. But
Prime Minister Persson has already been escorted by special security police
into his bulletproof Volvo....
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html