Jump to content

Recommended Posts

Posted
(defun c:test (/ a)
(setq a 1)
(mapcar (function (lambda (x)(+ x a))) '(1 2 3))
)

Hi Everyone, upon debugging the code above in VLIDE, why does the local variable 'a became global variable? Is there any way to prevent this? This happens to all that has lambda functions with local variable in it.

Posted

That's not a global variable. Lambda is defined inside test. But lambda doesn't redefine the variable a, so it can access a from its environment, meaning from the function test. After the execution of test terminates, the value of a is destroyed.

Posted

thats because the lambda function is inside your test function and as fuccaro said not redefining it.

You could also localize a within lambda (lambda (x / a)..)

Posted

[CHECKING TEXT <Untitled-0> loading...]
.
; === Top statistic:
; Global variables: (A)
; Function definition (with number of arguments): ((C:TEST . 0))
; Check done.

 

Hi fuccaro, my concern is when you have a lot of lambda functions it is hard to pin point which is which are now the local variables when checking text in editor becomes global variable.

Posted
1 hour ago, EnM4st3r said:

thats because the lambda function is inside your test function and as fuccaro said not redefining it.

You could also localize a within lambda (lambda (x / a)..)

Hi EnM4st3r, in some cases this would be fine, but usually I set the variable first on the main function rather than on the lambda function.

Posted (edited)
18 hours ago, LanloyLisp said:
(defun c:test (/ a)
(setq a 1)
(mapcar (function (lambda (x)(+ x a))) '(1 2 3))
)

Hi Everyone, upon debugging the code above in VLIDE, why does the local variable 'a became global variable? Is there any way to prevent this? This happens to all that has lambda functions with local variable in it.

For some reason the VLIDE reports it as a global but if you type in !a in the command line after running your test lisp, it should return:

 

Command: !a
nil

 

image.png.a74ad7780d264b31c5e4b0fca54a51a7.png

Edited by ronjonp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...