help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: random predicate function


From: Pascal J. Bourguignon
Subject: Re: random predicate function
Date: Mon, 13 Dec 2010 18:48:19 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

Ted Zlatanov <tzz@lifelogs.com> writes:

> On Mon, 13 Dec 2010 16:26:03 +0100 "Pascal J. Bourguignon" 
> <pjb@informatimago.com> wrote: 
>
> PJB> You could instead put your paragraphs in a vector and use:
>
> PJB> (defun shuffle (vector)
> PJB>   "Re-orders randomly the vector."
> PJB>   (loop
> PJB>       for i from (1-  (length vector)) downto 1
> PJB>       do (rotatef (aref vector i) (aref vector (random i)))))
>
> PJB> to shuffle them and then re-insert them.
>
> I noticed this function in lisp/play/cookie1.el which doesn't require
> CL:
>
> ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
> ; [of the University of Birmingham Computer Science Department]
> ; for the iterative version of this shuffle.
> ;
> ;;;###autoload
> (defun shuffle-vector (vector)
>   "Randomly permute the elements of VECTOR (all permutations equally likely)."
>   (let ((i 0)
>       j
>       temp
>       (len (length vector)))
>     (while (< i len)
>       (setq j (+ i (random (- len i))))
>       (setq temp (aref vector i))
>       (aset vector i (aref vector j))
>       (aset vector j temp)
>       (setq i (1+ i))))
>   vector)

This is a clear demonstration of the power of macros, and the goodness
of Common Lisp which includes a more powerful set of predefined macros
than any other remaining lisp.


> I wonder if it should be moved out of cookie1.el.



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]