Subject: Re: input
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 22 Apr 2009 02:45:58 -0500
Newsgroups: comp.lang.lisp
Message-ID: <W4OdnVOHf9-rVnPUnZ2dnUVZ_rOdnZ2d@speakeasy.net>
david  <notmas@gmail.com> wrote:
+---------------
| anonymous.c.lis...@gmail.com wrote:
| > He is talking about doing something like:
| >   (read-from-string (format nil "(~a)" "go north"))
| > produces:
| >   (go north)
| > Which in turn gets evaluated.
| > You convert the string into a lisp expression, then read it.
...
| thanks. it never occurred to me to use format like that.
| i was going to write a function to do that.
+---------------

I tend to prefer this sort of style to that FORMAT hack:

    > (with-input-from-string (s "go north")
	(loop with eof = (list :eof)
	      for form = (read s nil eof)
	      until (eq form eof)
	  collect form))

    (GO NORTH)
    > 

It gives you an easier handle on the intermediate forms, so you
can do some special processing, if you like, before (or instead of)
calling EVAL, e.g., rewriting CL:GO [which is what the above "GO"
really is!!] into MY-APP::MY-GO or something.

By the way, you might find OPFR[1] useful for this sort of thing:

    $ opfr
    opfr> get-universal-time     ; Functions look like "commands"

    3449374524
    opfr> expt 2 100

    1267650600228229401496703205376
    opfr> list 1 2 :foo 4

    (1 2 :FOO 4)
    opfr> 37     ; Rewrites to avoid FUNCALLing single non-function values.

    37
    opfr> most-positive-fixnum   ; Ditto)

    536870911
    opfr> 


-Rob

[1] http://rpw3.org/hacks/lisp/opfr.lisp      The library.
    http://rpw3.org/hacks/lisp/opfr.cmucl     Wrapper script for CMUCL.
    http://rpw3.org/hacks/lisp/opfr.clisp     Wrapper script for CLISP.

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607