[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: random predicate function
From: |
Ted Zlatanov |
Subject: |
Re: random predicate function |
Date: |
Wed, 15 Dec 2010 08:51:14 -0600 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux) |
On Mon, 13 Dec 2010 18:48:19 +0100 "Pascal J. Bourguignon"
<pjb@informatimago.com> wrote:
PJB> 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)
PJB> This is a clear demonstration of the power of macros, and the goodness
PJB> of Common Lisp which includes a more powerful set of predefined macros
PJB> than any other remaining lisp.
I agree the CL macros are nicer, but the non-macro version is much
easier to debug *in GNU Emacs* and it's already installed. So they both
have benefits.
Can your `shuffle' be part of cl-extra.el in Emacs? I like the brevity
of it, and it's a good example of how to use `loop' properly.
Ted
- Re: random predicate function, Pascal J. Bourguignon, 2010/12/13
- Re: random predicate function, Ted Zlatanov, 2010/12/13
- Re: random predicate function, Pascal J. Bourguignon, 2010/12/13
- Re: random predicate function,
Ted Zlatanov <=
- Re: random predicate function, Pascal J. Bourguignon, 2010/12/15
- Re: random predicate function, Ted Zlatanov, 2010/12/15
- Re: random predicate function, Pascal J. Bourguignon, 2010/12/15
- Re: random predicate function, Ted Zlatanov, 2010/12/15
- RE: random predicate function, Drew Adams, 2010/12/15