[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Scope let

SergioBG BG

7/14/2015 9:41:00 PM

Hi all , I have one question with this code , why x is not 11? , the scope of x is not correct? is inside of let block.

(funcall
(let ((x 10))
#'(lambda()
(+ 1 x)
(format t "~d" x))))
8 Answers

Antsan

7/14/2015 10:01:00 PM

0

Am Dienstag, 14. Juli 2015 23:41:03 UTC+2 schrieb SergioBG BG:
> Hi all , I have one question with this code , why x is not 11? , the scope of x is not correct? is inside of let block.
>
> (funcall
> (let ((x 10))
> #'(lambda()
> (+ 1 x)
> (format t "~d" x))))

The scope is correct, but your semantics aren't. (+ 1 x) just computes 11 and
then discards the result. '+' does not modify anything.
What you wanted to do is this:
(funcall
(let ((x 10))
#'(lambda ()
(incf x) ; or (setf x (+ 1 x))
(format t "~d" x))))

SergioBG BG

7/14/2015 10:04:00 PM

0

El martes, 14 de julio de 2015, 23:41:03 (UTC+2), SergioBG BG escribió:
> Hi all , I have one question with this code , why x is not 11? , the scope of x is not correct? is inside of let block.
>
> (funcall
> (let ((x 10))
> #'(lambda()
> (+ 1 x)
> (format t "~d" x))))

Wooow i see the problem , is that x is not rebinding with setf , and print first value of x.
Is correct?

Jeff Barnett

7/14/2015 10:06:00 PM

0

SergioBG BG wrote on 7/14/2015 3:40 PM:
> Hi all , I have one question with this code , why x is not 11? , the scope of x is not correct? is inside of let block.
>
> (funcall
> (let ((x 10))
> #'(lambda()
> (+ 1 x)
> (format t "~d" x))))
>
What do you think (+ 1 x) does in a statement context? You would
probably get an error if x were not a number. But what else?
--
Jeff Barnett

SergioBG BG

7/14/2015 10:08:00 PM

0

El miércoles, 15 de julio de 2015, 0:03:44 (UTC+2), SergioBG BG escribió:
> El martes, 14 de julio de 2015, 23:41:03 (UTC+2), SergioBG BG escribió:
> > Hi all , I have one question with this code , why x is not 11? , the scope of x is not correct? is inside of let block.
> >
> > (funcall
> > (let ((x 10))
> > #'(lambda()
> > (+ 1 x)
> > (format t "~d" x))))
>
> Wooow i see the problem , is that x is not rebinding with setf , and print first value of x.
> Is correct?
Antsan thanks ,I did not see your post but doing some tests on the problem came real.

SergioBG BG

7/14/2015 10:11:00 PM

0


They are tests to learn lisp , I'll settle for now there are no errors

Pascal J. Bourguignon

7/14/2015 10:13:00 PM

0

SergioBG BG <sergiobgar@gmail.com> writes:

> They are tests to learn lisp , I'll settle for now there are no errors

There's an interactive tutorial:
http://alarm.cti.depaul.edu/lispt...

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

SergioBG BG

7/15/2015 10:16:00 AM

0

El miércoles, 15 de julio de 2015, 0:12:39 (UTC+2), informatimago escribió:
> SergioBG BG <sergiobgar@gmail.com> writes:
>
> > They are tests to learn lisp , I'll settle for now there are no errors
>
> There's an interactive tutorial:
> http://alarm.cti.depaul.edu/lispt...
>
> --
> __Pascal Bourguignon__ http://www.informat...
> "The factory of the future will have only two employees, a man and a
> dog. The man will be there to feed the dog. The dog will be there to
> keep the man from touching the equipment." -- Carl Bass CEO Autodesk

Thnaks informatimago ;)

Mart van de Wege

7/15/2015 4:04:00 PM

0

Antsan <thomas.bartscher@gmail.com> writes:

> Am Dienstag, 14. Juli 2015 23:41:03 UTC+2 schrieb SergioBG BG:
>> Hi all , I have one question with this code , why x is not 11? , the scope of x is not correct? is inside of let block.
>>
>> (funcall
>> (let ((x 10))
>> #'(lambda()
>> (+ 1 x)
>> (format t "~d" x))))
>
> The scope is correct, but your semantics aren't. (+ 1 x) just computes 11 and
> then discards the result. '+' does not modify anything.
> What you wanted to do is this:
> (funcall
> (let ((x 10))
> #'(lambda ()
> (incf x) ; or (setf x (+ 1 x))
> (format t "~d" x))))

Alternatively, if you want to join the camp of 'setf is EEEEVILL!':

(funcall
(let ((x 10))
#'(lambda ()
(format t "~d" (+ 1 x)))))

Mart
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.