From: ·······@iwebworks.com
Subject: lisp newbie (2) - how do you evaluate an expression read in from a file
Date: 
Message-ID: <1141684421.960703.321560@i39g2000cwa.googlegroups.com>
Hi all,
Say I read in a file using "with-open-file" and READ the first
expression found, that happens to use 5 specific functions I DEFUN'd,
and I'm supposed to evaluate the expression.

But if I READ into a variable in LET*, call it my-expression, then I
can't execute that expression.  If my-expression is "(foo 7)" as read
from the file, I can't execute it by just (my-expression) because it
says that function is not defined.  I can (FORMAT t " ~S"
my-expression) it out to the screen, so I know it read successfully,
but I can't evaluate it.

Is there a way to evaluate the expression (which could be as complex as
any other Lisp expression) contained in that read-in line?

For any info, I would greatly appreciate it,
Thanks,
Dan

From: Kaz Kylheku
Subject: Re: lisp newbie (2) - how do you evaluate an expression read in from a file
Date: 
Message-ID: <1141685106.050039.255450@e56g2000cwe.googlegroups.com>
·······@iwebworks.com wrote:
> Hi all,
> Say I read in a file using "with-open-file" and READ the first
> expression found, that happens to use 5 specific functions I DEFUN'd,
> and I'm supposed to evaluate the expression.
>
> But if I READ into a variable in LET*, call it my-expression, then I
> can't execute that expression.  If my-expression is "(foo 7)" as read
> from the file, I can't execute it by just (my-expression) because it
> says that function is not defined.

That's right. A symbol in the first position of a compound form is
expected to be the name of a function, macro or special operator.  It
means nothing that it's a variable holding the source code of a Lisp
expression; no consideration is given to that.

>  I can (FORMAT t " ~S"
> my-expression) it out to the screen, so I know it read successfully,
> but I can't evaluate it.

That's done by the surprisingly named EVAL function. :)

(eval my-expression)

If you are going to be reading forms from a file and just evaluating
them straight, a better way to do that is to use the LOAD function that
is built into Lisp.
From: Patrick Frankenberger
Subject: Re: lisp newbie (2) - how do you evaluate an expression read in from a file
Date: 
Message-ID: <duiggd$6o1$03$1@news.t-online.com>
·······@iwebworks.com wrote:
> Say I read in a file using "with-open-file" and READ the first
> expression found, that happens to use 5 specific functions I DEFUN'd,
> and I'm supposed to evaluate the expression.
> 
> But if I READ into a variable in LET*, call it my-expression, then I
> can't execute that expression.  If my-expression is "(foo 7)" as read
> from the file, I can't execute it by just (my-expression) because it
> says that function is not defined.

Another way to execute it (not using eval) is:
(let* ((lambda-expression (list 'lambda () my-expression))
        (function (compile nil lambda-expression)))
           (funcall function))

HTH,
Patrick
From: Eli Gottlieb
Subject: Re: lisp newbie (2) - how do you evaluate an expression read in from a file
Date: 
Message-ID: <YcLPf.7136$Zs1.540@twister.nyroc.rr.com>
Patrick Frankenberger wrote:
> ·······@iwebworks.com wrote:
> 
>> Say I read in a file using "with-open-file" and READ the first
>> expression found, that happens to use 5 specific functions I DEFUN'd,
>> and I'm supposed to evaluate the expression.
>>
>> But if I READ into a variable in LET*, call it my-expression, then I
>> can't execute that expression.  If my-expression is "(foo 7)" as read
>> from the file, I can't execute it by just (my-expression) because it
>> says that function is not defined.
> 
> 
> Another way to execute it (not using eval) is:
> (let* ((lambda-expression (list 'lambda () my-expression))
>        (function (compile nil lambda-expression)))
>           (funcall function))
> 
> HTH,
> Patrick
That works, but is there a way to do the execution within the current 
lexical environment?  If only you could use (function ...) on computed 
lambda expressions or variables containing forms.

-- 
The science of economics is the cleverest proof of free will yet 
constructed.
From: ·············@gmail.com
Subject: Re: lisp newbie (2) - how do you evaluate an expression read in from a file
Date: 
Message-ID: <1141772412.142084.136290@i40g2000cwc.googlegroups.com>
> Hi all,
> Say I read in a file using "with-open-file" and READ the first

(eval (read-from-string big-string-here))

example:

>(setf x "(print 'me)")
"(print 'me)"
>x
"(print 'me)"
>(eval x)
"(print 'me)"
>(eval (read-from-string x))
ME
ME