Re: Rebuilding functions at run-time
- From: Tamas K Papp <tkpapp@xxxxxxxxx>
- Date: 15 Apr 2009 21:31:27 GMT
On Wed, 15 Apr 2009 22:44:27 +0200, Francisco Vides Fernández wrote:
Tamas K Papp wrote:
On Wed, 15 Apr 2009 21:34:36 +0200, Francisco Vides Fernández wrote:
Hello
Let's suppose that I have a file named "foo" with just one word "bar".
I've written a macro, deftemplate, that expands to something like:
(deftemplate foo () #p"foo")
=>
(defun foo ()
(write-string "bar"))
So far, so good. Now I'd like to add some code that, when the contents
of "foo" changes to "baz", should rebind #'foo to
(lambda ()
(write-string "baz"))
But, AFAIK, this can't be a macro, since it will be expanded at macro-
expansion time, so the function it compiles will depend on the
contents of the file at expansion time.
I guess I could use eval, but everybody says is evil (honestly, I
still can't see why).
Another option could be compile, I suppose.
What is the Lisp Way to do this? (if there is such)
It is not entirely clear what you are trying to achieve, but you could
use closures.
(defun make-function (initial-string)
(let ((string initial-string))
(list (lambda () ; writes string
(write-string string))
(lambda (new-string) ; sets string
(setf string new-string)))))
(defparameter *foo* (make-function "foo")) (funcall (first *foo*)) ;
"foo"
(funcall (second *foo*) "bar") ; which you can read from a file, etc
(funcall (first *foo*)) ; "bar"
I'm afraid that I oversimplified the problem. Let's try again (please be
patient :)) If file 'foo' contained one string in one line then I want
to generate the function
(lambda ()
(write-string "bar"))
But if contains two strings in two separated lines then I'd like to
generate
(lambda ()
(write-string "bar")
(write-string "baz"))
So, I think that the closures solution wouldn't work for me, as I can
generate the lambda function body only by using a macro, or evaling or
compiling. Am I right?
You can always construct such forms and then compile them (see compile
and eval). But that is the most un-Lispy way of doing things you can
imagine.
You are telling us _how_ you want to do something, but that is clearly
wrong. Tell us _what_ you want to do. Closures provide the
functionality you need, and are idiomatic in Lisp.
Tamas
.
- References:
- Rebuilding functions at run-time
- From: Francisco Vides =??B?RmVybsOhbmRleg==?=
- Re: Rebuilding functions at run-time
- From: Tamas K Papp
- Re: Rebuilding functions at run-time
- From: Francisco Vides =??B?RmVybsOhbmRleg==?=
- Rebuilding functions at run-time
- Prev by Date: Re: How useful is "trace"
- Next by Date: Re: Rebuilding functions at run-time
- Previous by thread: Re: Rebuilding functions at run-time
- Next by thread: Re: Rebuilding functions at run-time
- Index(es):
Relevant Pages
|