From: Matias GUIJARRO
Subject: Why this doesn't work?
Date: 
Message-ID: <39E31E3B.634A8CD8@utbm.fr>
I'm a french student in an engineering school.
I'm currently studying LISP, and I write and test my code under Emacs.
I've found the following example on a book dedicated to LISP, but
unfortunately it doesn't seem to work :

(setf power-of-two
      (let ((previous-power-of-two))
           #'(lambda ()
                 (setf previous-power-of-two (* previous-power-of-two
2)))))

(funcall power-of-two) => 2
(funcall power-of-two) => 4
... etc

Emacs retrieves the message :
symbol's value as variable is void : previous-power-of-two

I can't understand why, it's just like the "let" thing is ignored ?

I'll be very pleased if some1 can give me an answer...

Regards,
Matias GUIJARRO.

From: Barry Margolin
Subject: Re: Why this doesn't work?
Date: 
Message-ID: <MmFE5.3$xe3.164@burlma1-snr2>
In article <·················@utbm.fr>,
Matias GUIJARRO  <···············@utbm.fr> wrote:
>I'm a french student in an engineering school.
>I'm currently studying LISP, and I write and test my code under Emacs.
>I've found the following example on a book dedicated to LISP, but
>unfortunately it doesn't seem to work :
>
>(setf power-of-two
>      (let ((previous-power-of-two))
>           #'(lambda ()
>                 (setf previous-power-of-two (* previous-power-of-two
>2)))))
>
>(funcall power-of-two) => 2
>(funcall power-of-two) => 4
>... etc
>
>Emacs retrieves the message :
>symbol's value as variable is void : previous-power-of-two
>
>I can't understand why, it's just like the "let" thing is ignored ?
>
>I'll be very pleased if some1 can give me an answer...

You're trying to create a lexical closure, but Emacs Lisp is dynamically
scoped.  If you're studying Common Lisp, you should get yourself a real CL
implementation, rather than trying to use Emacs Lisp.  However, to get an
emulation of Common Lisp's lexical bindings, you can do:

(require 'cl)

and then use "lexical-let" instead of "let".

Note also that you should be initializing previous-power-of-two to 1; the
way you've written the code above, even in Common Lisp it would have gotten
an error, complaining that the arguments to * were not the correct type.

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Michael J Short
Subject: Re: Why this doesn't work?
Date: 
Message-ID: <39E46ABE.85347AAF@tothemoon.freeserve.co.uk>
here try this


(setf power-of-two
      (let ((previous-power-of-two 1))
           #'(lambda ()
                 (setf previous-power-of-two (* previous-power-of-two
2)))))

not at lot of diffrence as you can see, yoou only needed to initialize it
to one, as 2 * 0 is still zero ye.  good look.

any other questions then please ask.


Matias GUIJARRO wrote:

> I'm a french student in an engineering school.
> I'm currently studying LISP, and I write and test my code under Emacs.
> I've found the following example on a book dedicated to LISP, but
> unfortunately it doesn't seem to work :
>
> (setf power-of-two
>       (let ((previous-power-of-two))
>            #'(lambda ()
>                  (setf previous-power-of-two (* previous-power-of-two
> 2)))))
>
> (funcall power-of-two) => 2
> (funcall power-of-two) => 4
> ... etc
>
> Emacs retrieves the message :
> symbol's value as variable is void : previous-power-of-two
>
> I can't understand why, it's just like the "let" thing is ignored ?
>
> I'll be very pleased if some1 can give me an answer...
>
> Regards,
> Matias GUIJARRO.
From: Matias GUIJARRO
Subject: Lisp newsgroup
Date: 
Message-ID: <39E553CE.7302ACCC@utbm.fr>
Michael J Short wrote:

> here try this
>
> (setf power-of-two
>       (let ((previous-power-of-two 1))
>            #'(lambda ()
>                  (setf previous-power-of-two (* previous-power-of-two
> 2)))))
>
> not at lot of diffrence as you can see, yoou only needed to initialize it
> to one, as 2 * 0 is still zero ye.  good look.
>
> any other questions then please ask.

In fact I've just realized i've made a typo, and of course I need the
(let ((previous-power-of-two 1)) statement, as you've written.
There is no problem with this, even with the right let, it stills not
work... Same error as I've already specified.
If you've time to spent, try it you'll see it doesn't work.
Otherwise, if it works for you, which version of Emacs are u using????

Thanks in advance.

Regards,
Matias GUIJARRO.
From: Barry Margolin
Subject: Re: why this doesn't work
Date: 
Message-ID: <StlF5.9$OD4.249@burlma1-snr2>
In article <·················@utbm.fr>,
Matias GUIJARRO  <···············@utbm.fr> wrote:
>Michael J Short wrote:
>
>> here try this
>>
>> (setf power-of-two
>>       (let ((previous-power-of-two 1))
>>            #'(lambda ()
>>                  (setf previous-power-of-two (* previous-power-of-two
>> 2)))))
>>
>> not at lot of diffrence as you can see, yoou only needed to initialize it
>> to one, as 2 * 0 is still zero ye.  good look.
>>
>> any other questions then please ask.
>
>In fact I've just realized i've made a typo, and of course I need the
>(let ((previous-power-of-two 1)) statement, as you've written.
>There is no problem with this, even with the right let, it stills not
>work... Same error as I've already specified.
>If you've time to spent, try it you'll see it doesn't work.
>Otherwise, if it works for you, which version of Emacs are u using????

Did you see my post where I explained that Emacs Lisp doesn't do lexical
binding?  I think the other people who responded to you didn't realize you
were trying to do this in Emacs Lisp, so they gave Common Lisp solutions.

P.S. Why did you change the subject of the thread from "why this doesn't
work" to "Lisp newsgroup"?

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Matias GUIJARRO
Subject: Re: Why this doesn't work?
Date: 
Message-ID: <39E55407.6852E195@utbm.fr>
> I've made a typo, you should read :

>
> (setf power-of-two
>       (let ((previous-power-of-two 1)) of course !
>            #'(lambda ()
>                  (setf previous-power-of-two (* previous-power-of-two
> 2)))))
From: Michael J Short
Subject: Re: Why this doesn't work?
Date: 
Message-ID: <39E5CA3F.F2DEDD67@tothemoon.freeserve.co.uk>
CL-USER 1 > funcall power-of-two)
2

CL-USER 2 > funcall power-of-two
4

here is what I get when I run it on freelisp, as you can see it works.

harlequin freelisp.  have a look around you will be able to get a version


Matias GUIJARRO wrote:

> > I've made a typo, you should read :
>
> >
> > (setf power-of-two
> >       (let ((previous-power-of-two 1)) of course !
> >            #'(lambda ()
> >                  (setf previous-power-of-two (* previous-power-of-two
> > 2)))))