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

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

Re: Run a program


From: Deniz Dogan
Subject: Re: Run a program
Date: Tue, 27 Sep 2011 23:06:54 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2

On 2011-09-27 07:28, Roland Thiers wrote:
Hi all,
I am a novice programming in Emacs Lisp.
My question :
I dit this program :
(defun coins (n)
(let ((i n) (repartition [0 0 0]) (c1 0) (c2 0) (r 0))
(while (> i 0)
(setq c1 (random 2))
(setq c2 (random 2))
(setq r (+ c1 c2))
(aset repartition r (1+ (aref repartition r)))
(setq i (- i 1)))
(message " repartition %s " repartition)))

aim : you toss two coins, count tails : 0 or 1 or 2. Repeat 10 times or
more and see what happens.

If I evaluate this program with C-x C-e and do (coins 10) C-x C-e it
works fine, I get some thing like
[3 5 2].
But if I repeat C-x C-e after (coins 10) I get some thing like [5 11 4]
then [9 15 6] ...
I can't understand that !
Could some one help me ?

I am not exactly sure why that happens, but it seems like you're referencing the same vector after each call. To make sure the vector is fresh before each run, replace:

(repartition [0 0 0])

with:

(repartition (make-vector 3 0))



reply via email to

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