Re: Quick question: eval and read-from-string scoping



Jeremy Smith wrote:
Hi,

When I run this code in CLisp:

    	(let ((left 5) (top 10))
    	    	(eval(read-from-string "(eq left (margin top 20))")))

When executed, it complains that "*** - EVAL: variable left has no value". At first I wondered why the local variables were not in scope for the eval'd code and was going to post, but I found this thread:

http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/846c4f
53e91ef251/69239105712fed0d?lnk=st&q=lisp+eval+global+variables&rnum=2#
69239105712fed0d

And now I realise that eval is like a function, and local variables are not passed from function to function. So the question is revised: How do I pass variables to code executed by eval without declaring them as special?


For the above example simply do,

(let ((left 5) (top 10))
  (eval `(let ((left ,left) (top ,top))
           (eql left (margin top 20)))))

If you want to continue to use read-from-string like the
above example you can do,

(let ((left 5) (top 10))
  (eval `(let ((left ,left) (top , top))
           ,(read-from-string "(eq left (margin top 20))"))))

Wade

.



Relevant Pages

  • Re: Pyserial again
    ... Richie Hindle wrote: ... at this point we're getting into application design issues unless and until Luca can learn enough Python and other programming stuff to understand why "ser" is "going out of scope" ... If you don't know about object construction and destruction, memory allocation and garbage collection, the concept of "scope" and the difference between things like local variables, global variables, and attributes, we're going to continue to have an exceptionally difficult and frustrating time helping you. ...
    (comp.lang.python)
  • Re: To Hungarian or not to Hungarian
    ... prefix that denotes whether the item is a parameter, ... The type, but not the scope. ... I personally do not use prefixes for local variables. ... "Death is one of the few things that can be done as easily lying ...
    (borland.public.delphi.non-technical)
  • Re: VB 6.0 Desktop - Global Variables
    ... variables are not "slower" than local variables. ... wide scope", then this kind of variables only can live in a module. ... standard code module. ... without needing a reference to an object. ...
    (microsoft.public.vb.general.discussion)
  • Re: Local variables - quick question
    ... You can't use a variable which isn't in scope within ... You could declare an instance variable i, ... and after that I would like to use int i - new declaration. ... language is defined to prevent local variables from hiding each other. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: very strange effect with anonymous delegates (.net 2.0)
    ... > the view of the sourcecode since "PropertyInfo prop" is a readonly ... > they should at least throw a compiler error when a foreach iterator is ... at the outermost scope of the local variables that are used within the anon ... It obviously can't do it within the scope as the outermost one ...
    (microsoft.public.dotnet.languages.csharp)