From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.help Subject: Re: random predicate function Date: Wed, 15 Dec 2010 08:51:14 -0600 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <8739pz144d.fsf@lifelogs.com> References: <87mxo9r8xg.fsf@kuiper.lan.informatimago.com> <87r5dlmw4a.fsf@lifelogs.com> <8762uxr2cc.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1292429342 22716 80.91.229.12 (15 Dec 2010 16:09:02 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 15 Dec 2010 16:09:02 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 15 17:08:58 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PStuS-00056P-Oy for geh-help-gnu-emacs@m.gmane.org; Wed, 15 Dec 2010 17:08:56 +0100 Original-Received: from localhost ([127.0.0.1]:37680 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PStdr-0003jC-6B for geh-help-gnu-emacs@m.gmane.org; Wed, 15 Dec 2010 10:51:47 -0500 Original-Path: usenet.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!t-online.de!news.albasani.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 49 Original-X-Trace: news.albasani.net v4v8n7xUZzd23A3P47ihFjm8JeevLYVPrcsWyiNnVDoMxmPf+Z1P3CEdmYP/QF0J7F7ns9DiivqaSpSuhTwS0Nf9Al8jyAGqnV6Nm9nS/EeZXifFcB3+TmH0iN1Kd8VO Original-NNTP-Posting-Date: Wed, 15 Dec 2010 14:51:14 +0000 (UTC) X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" Cancel-Lock: sha1:Uz7okKAFew/RDSpezjfzKwwbGlU= sha1:S2B9OdqoijMwAwhXYl/yH/hnwTQ= User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux) Injection-Info: news.albasani.net; logging-data="mSIVX+1haASwFqNdj+baiiGpAL8eYWuPdfWP/CpCCg6yJHQNld0fi8ouFFZbnjJ5EditYa+CCZIC5OFY84aHIpl6fHWBJR/tXBmUxzk0Gn7oECfrCwGf2+q0jY/K6KXx"; mail-complaints-to="abuse@albasani.net" Original-Xref: usenet.stanford.edu gnu.emacs.help:183292 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:77542 Archived-At: On Mon, 13 Dec 2010 18:48:19 +0100 "Pascal J. Bourguignon" wrote: PJB> Ted Zlatanov writes: >> On Mon, 13 Dec 2010 16:26:03 +0100 "Pascal J. Bourguignon" 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 >> ; [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