Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Q]How to evaluate a string as lisp code?

545 views
Skip to first unread message

Yeh, Chih Hao

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to
I am new to lisp, and is writing a application mapping some tags into lisp
codes. I heard LISP can run-time interpret string as code.
I can build strings which contains legal lisp codes now, but I don't know
how to make the string run. Any suggestions?
Yeh, Chih Hao

David J. Cooper

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to

Let's say your string is in the variable called string.

(eval (read-from-string string))

This will evaluate exactly one expression from the string.
You'll have to do something more (probably with subseq or
something) to read and evaluate multiple expressions from
a string.

Also, make absolutely sure that you really have to wait until
runtime to do this. The use of eval sends up a red flag because
newbies will sometimes slip into using it when they are probably
trying to do something they really don't know how to do yet, and
which might better be accomplished with using macros, funcall, or
apply.

But it sounds like you are trying to interpret XML as code or
something, in which case i suppose the use of eval might be
appropriate, but i really don't know much about XML yet.

Yours,

-dave

--
David J. Cooper Jr, Chief Engineer Genworks International
dco...@genworks.com 5777 West Maple, Suite 130
(248) 932-2512 (Genworks HQ/voicemail) West Bloomfield, MI 48322-2268
(248) 407-0633 (pager) http://www.genworks.com

David J. Cooper

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to
"Yeh, Chih Hao" wrote:
>
> I am new to lisp, and is writing a application mapping some tags into lisp
> codes. I heard LISP can run-time interpret string as code.
> I can build strings which contains legal lisp codes now, but I don't know
> how to make the string run. Any suggestions?
> Yeh, Chih Hao

Also, take a good hard look at why you are building strings containing
code, rather than just building expressions directly, with ` and , (backquote
and comma). If this is possible you will probably find it much more
convenient, and it is compatible with the usual way of doing things
with macros.

Alex Henderson

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to
> Let's say your string is in the variable called string.
>
> (eval (read-from-string string))
>
> This will evaluate exactly one expression from the string.
> You'll have to do something more (probably with subseq or
> something) to read and evaluate multiple expressions from
> a string.

Better still, use a string-stream:

(do ((stream (make-string-input-stream string))
read-list)
((not (listen stream)) (reverse read-list))
(push (eval (read stream)) read-list))

A

--
Writer: Someone who's never seen a chasm that didn't yawn.

Pierre R. Mai

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to
Alex Henderson <ac...@hermes.cam.ac.uk> writes:

> > Let's say your string is in the variable called string.
> >
> > (eval (read-from-string string))
> >
> > This will evaluate exactly one expression from the string.
> > You'll have to do something more (probably with subseq or
> > something) to read and evaluate multiple expressions from
> > a string.
>
> Better still, use a string-stream:
>
> (do ((stream (make-string-input-stream string))
> read-list)
> ((not (listen stream)) (reverse read-list))
> (push (eval (read stream)) read-list))

For fans of loop:

(with-input-from-string (stream string)
(loop with eof-value = (gensym)
for expression = (read stream nil eof-value)
while (not (eq expression eof-value))
collect (eval expression)))

Regs, Pierre.

--
Pierre Mai <pm...@acm.org> PGP and GPG keys at your nearest Keyserver
"One smaller motivation which, in part, stems from altruism is Microsoft-
bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]

Erik Naggum

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to
* "David J. Cooper" <dco...@genworks.com>

| Let's say your string is in the variable called string.
|
| (eval (read-from-string string))
|
| This will evaluate exactly one expression from the string. You'll have
| to do something more (probably with subseq or something) to read and
| evaluate multiple expressions from a string.

note that read-from-string returns two values, the secondary of which is
the position from which you could continue to read from the string.

#:Erik

Hartmann Schaffer

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to
In article <8bs26b$ocv$1...@gemini.ntu.edu.tw>,

"Yeh, Chih Hao" <r672...@im.ntu.edu.tw> writes:
> I am new to lisp, and is writing a application mapping some tags into lisp
> codes. I heard LISP can run-time interpret string as code.
> I can build strings which contains legal lisp codes now, but I don't know
> how to make the string run. Any suggestions?

if you build only one impression: use eval and with-input-from-string.
on the other hand, wouldn't it be easier to build a list rather than a
string?

--

Hartmann Schaffer


0 new messages