all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* 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

* Re: Run a program
  2011-09-27  5:28 Run a program Roland Thiers
@ 2011-09-27 21:06 ` Deniz Dogan
  2011-09-28  1:43 ` Le Wang
  1 sibling, 0 replies; 6+ messages in thread
From: Deniz Dogan @ 2011-09-27 21:06 UTC (permalink / raw)
  To: help-gnu-emacs

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



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

* 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

* Re: Run a program
  2011-09-27  5:28 Run a program Roland Thiers
  2011-09-27 21:06 ` Deniz Dogan
@ 2011-09-28  1:43 ` Le Wang
  2011-09-28  9:15   ` Roland Thiers
  1 sibling, 1 reply; 6+ messages in thread
From: Le Wang @ 2011-09-28  1:43 UTC (permalink / raw)
  To: Roland Thiers; +Cc: help-gnu-emacs

On Tue, Sep 27, 2011 at 1:28 PM, Roland Thiers <roland.thiers@canl.nc> wrote:
> Hi all,
> I am a novice programming in Emacs Lisp.
> My question :

This is a quoting issue.  It got me a short while ago.  Read the whole
thread, especially when I reach enlightenment later on.
http://lists.gnu.org/archive/html/bug-gnu-emacs/2011-09/msg00457.html

TLDR; instead of using [x y z] use (vector x y z).  Never modify
things you quote or you will be confused.  Only quote constants.

-- 
Le



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

* Re: Run a program
  2011-09-28  1:43 ` Le Wang
@ 2011-09-28  9:15   ` Roland Thiers
  2011-09-28 13:44     ` Drew Adams
  0 siblings, 1 reply; 6+ messages in thread
From: Roland Thiers @ 2011-09-28  9:15 UTC (permalink / raw)
  To: help-gnu-emacs

Thank very much Deniz, Kevin and Le for your replies and advices.
(vector 0 0 0) and (make-vector 3 0) work well.
I understood that my problem was connected with quoting, now I have to  
read  carefully
the thread : buffer-local variables seem to remember values  
(explanations of Drew Adams and others). Not obvious, and my poor  
english
does not help me !
Roland
Le 28 sept. 11 à 12:43, Le Wang a écrit :

> On Tue, Sep 27, 2011 at 1:28 PM, Roland Thiers  
> <roland.thiers@canl.nc> wrote:
>> Hi all,
>> I am a novice programming in Emacs Lisp.
>> My question :
>
> This is a quoting issue.  It got me a short while ago.  Read the whole
> thread, especially when I reach enlightenment later on.
> http://lists.gnu.org/archive/html/bug-gnu-emacs/2011-09/msg00457.html
>
> TLDR; instead of using [x y z] use (vector x y z).  Never modify
> things you quote or you will be confused.  Only quote constants.
>
> -- 
> Le




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

* RE: Run a program
  2011-09-28  9:15   ` Roland Thiers
@ 2011-09-28 13:44     ` Drew Adams
  0 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2011-09-28 13:44 UTC (permalink / raw)
  To: 'Roland Thiers', help-gnu-emacs

> Thank very much Deniz, Kevin and Le for your replies and advices.
> (vector 0 0 0) and (make-vector 3 0) work well.
> I understood that my problem was connected with quoting, now 
> I have to read  carefully the thread : buffer-local variables
> seem to remember values (explanations of Drew Adams and others).
> Not obvious,

No, it's not obvious to someone new to Lisp.

To be clear, Le took a shortcut in saying that this has to do with quoting.  In
fact, it does not.

What it comes down to is that the Lisp _reader_ constructs Lisp objects as it
reads sexps (code).  That includes lists, vectors, and symbols.  Nearly every
Lisp sexp you see in a program leads to the Lisp reader constructing a list of
lists, symbols, vectors, strings, etc.  (defun foo ...) becomes a list when it
is read, and so on.

Reading is a separate step from evaluating.  Lisp interpretation is a loop with
these steps: Read (a sexp), Evaluate it, Print (the result) - the so-called REP
Loop, or REPL.  People sometimes gloss over the Read step when thinking about
code, but that can be a mistake, as the gotcha in question shows.
http://en.wikipedia.org/wiki/Read-eval-print_loop

A special form such as `quote' does not receive program text as its argument; it
receives an internal Lisp object already created by the Lisp reader.

Depending on the particular Lisp and its implementation, the reader might or
might not create a new, different list or vector each time it encounters
equivalent text (sexp).  In Emacs Lisp, it typically (always?) does not create a
new one - it reuses the same list or vector.

And that is why you should not modify such a "constant", at least not expecting
a new, distinct constant to have been created at each textual occurrence of the
same sexp.  Those textual occurrences will typically all stand for the same Lisp
object.

This is a gotcha associated with learning Lisp, but once it is learned you have
a better understanding of the kind of language it is.

HTH.




^ 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  5:28 Run a program 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
  -- strict thread matches above, loose matches on Subject: below --
2011-09-27 22:01 Buchs, Kevin

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.