all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: Run a program
@ 2011-09-27 22:01 Buchs, Kevin
  0 siblings, 0 replies; 6+ messages in thread
From: Buchs, Kevin @ 2011-09-27 22:01 UTC (permalink / raw
  To: help-gnu-emacs

Roland, 

Please consider using some indentation in your code postings, as we are left swimming in the parens if you don't. 

It is also inexplicable by me that repartition is maintaining a value, despite the let form. It is not recognized globally. You can see that with the additional message form I inserted. Then in addition, I did explicit initialization of each element.

(defun coins (n)
   (let 
      (  (i n) 
         (repartition [0 0 0])
         (c1 0)
         (c2 0)
         (r 0)) 
  
      (message "repartition starts at: %s " repartition)
      (aset repartition 0 0)
      (aset repartition 1 0)
      (aset repartition 2 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)))

Kevin Buchs   |  Senior Engineer  |  Department of Physiology and Biomedical Engineering - SPPDG
507-538-5459  |   buchs.kevin@mayo.edu  |  http://www.mayo.edu/sppdg
Mayo Clinic  |  200 1st St. SW  |  Rochester, MN 55905  


> (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)))


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))



^ permalink raw reply	[flat|nested] 6+ messages in thread
* Run a program
@ 2011-09-27  5:28 Roland Thiers
  2011-09-27 21:06 ` Deniz Dogan
  2011-09-28  1:43 ` Le Wang
  0 siblings, 2 replies; 6+ messages in thread
From: Roland Thiers @ 2011-09-27  5:28 UTC (permalink / raw
  To: help-gnu-emacs

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 ?
Roland Thiers
Noumea New Caledonia
roland dot thiers at canl dot nc




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-09-28 13:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-27 22:01 Run a program Buchs, Kevin
  -- strict thread matches above, loose matches on Subject: below --
2011-09-27  5:28 Roland Thiers
2011-09-27 21:06 ` Deniz Dogan
2011-09-28  1:43 ` Le Wang
2011-09-28  9:15   ` Roland Thiers
2011-09-28 13:44     ` Drew Adams

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.