LanloyLisp Posted February 21 Posted February 21 (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. Quote
fuccaro Posted February 21 Posted February 21 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. Quote
EnM4st3r Posted February 21 Posted February 21 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)..) Quote
LanloyLisp Posted February 21 Author Posted February 21 [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. Quote
LanloyLisp Posted February 21 Author Posted February 21 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. Quote
ronjonp Posted February 21 Posted February 21 (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 Edited February 21 by ronjonp Quote
Recommended Posts
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.