Re: Quick question: eval and read-from-string scoping
- From: Wade Humeniuk <whumeniu+anti+spam@xxxxxxxxx>
- Date: Thu, 29 Dec 2005 22:37:24 GMT
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
.
- References:
- Quick question: eval and read-from-string scoping
- From: Jeremy Smith
- Quick question: eval and read-from-string scoping
- Prev by Date: Re: Quick question: eval and read-from-string scoping
- Next by Date: Re: Quick question: eval and read-from-string scoping
- Previous by thread: Re: Quick question: eval and read-from-string scoping
- Next by thread: Re: Quick question: eval and read-from-string scoping
- Index(es):
Relevant Pages
|