From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: random predicate function
Date: Mon, 13 Dec 2010 18:48:19 +0100 [thread overview]
Message-ID: <8762uxr2cc.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: 87r5dlmw4a.fsf@lifelogs.com
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 {}.
next prev parent reply other threads:[~2010-12-13 17:48 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.0.1292251427.11097.help-gnu-emacs@gnu.org>
2010-12-13 15:26 ` random predicate function Pascal J. Bourguignon
2010-12-13 17:16 ` Ted Zlatanov
2010-12-13 17:48 ` Pascal J. Bourguignon [this message]
2010-12-15 14:51 ` Ted Zlatanov
2010-12-15 15:20 ` Pascal J. Bourguignon
2010-12-15 16:44 ` Ted Zlatanov
2010-12-15 17:28 ` Pascal J. Bourguignon
2010-12-15 18:39 ` Ted Zlatanov
2010-12-15 18:04 ` Drew Adams
2010-12-13 18:17 ` Tyler Smith
[not found] ` <mailman.2.1292264248.1009.help-gnu-emacs@gnu.org>
2010-12-13 18:38 ` Pascal J. Bourguignon
2010-12-13 19:05 ` Tyler Smith
2010-12-13 14:43 Tyler Smith
2010-12-13 15:37 ` Gary
2010-12-13 16:08 ` Tyler Smith
2010-12-13 16:16 ` Erik Iverson
2010-12-13 16:50 ` Tyler Smith
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8762uxr2cc.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.