From: Joakim Hove
Subject: Adding functions at runtime - understand eval
Date: 
Message-ID: <4y7kbghkfk.fsf@skjellgran.ii.uib.no>
<disclaimer>
This is related to emacs-lisp, I hope and think the problems the
question is sufficently general to be asked here, but if I am really
asking about some emacs idiosyncrasies please forgive me.
</disclaimer>

I have the following code:

  (defun create-function (defun-string)
    (eval defun-string))

  (defun test-it ()
    (create-function "(defun dynamic-function (arglist) (list 25))")
    (message "Dynamic function gives %s" (dynamic-function nil)))


Basically I have written a full function definition in a string, and
then eval this string. However when I subsequently try to call the
newly created function 'dynamic-function I just get the error:
"Symbol's function definition is void: dynamic function", so the eval
step has not worked.


Any suggestions greatly appreciated

Joakim Hove


-- 
Joakim Hove  / ····@ii.uib.no  /  (55 5) 84076

From: Matthew Danish
Subject: Re: Adding functions at runtime - understand eval
Date: 
Message-ID: <20030303044829.A20083@lain.cheme.cmu.edu>
On Mon, Mar 03, 2003 at 10:24:31AM +0100, Joakim Hove wrote:
> <disclaimer>
> This is related to emacs-lisp, I hope and think the problems the
> question is sufficently general to be asked here, but if I am really
> asking about some emacs idiosyncrasies please forgive me.
> </disclaimer>
> 
> I have the following code:
> 
>   (defun create-function (defun-string)
>     (eval defun-string))
> 
>   (defun test-it ()
>     (create-function "(defun dynamic-function (arglist) (list 25))")
>     (message "Dynamic function gives %s" (dynamic-function nil)))
> 
> 
> Basically I have written a full function definition in a string, and
> then eval this string. However when I subsequently try to call the
> newly created function 'dynamic-function I just get the error:
> "Symbol's function definition is void: dynamic function", so the eval
> step has not worked.

I highly suspect that Emacs-Lisp's EVAL expects an S-expression as
argument.  That means lists and atoms.  A string is an atom, and EVALing
a string results in a string.

I should interject at this point a note about how Lisp code is
represented by Lisp data structures.  For example, your test code would
be:

(list 'defun 'dynamic-function (list 'arglist) (list 'list 25))

or, in a structurally similar but more convenient fashion:

'(defun dynamic-function (arglist) (list 25))

Both of which will evaluate to the following data-structure (here in
printed form):

(defun dynamic-function (arglist) (list 25))

Applying EVAL to this result will probably achieve what you want.

If you want to turn a string into a Lisp S-expression data-structure,
then you should use the READ function, if it exists in Emacs-Lisp.

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
From: Matthew Danish
Subject: Re: Adding functions at runtime - understand eval
Date: 
Message-ID: <20030303045146.B20083@lain.cheme.cmu.edu>
On Mon, Mar 03, 2003 at 04:48:29AM -0500, Matthew Danish wrote:
> If you want to turn a string into a Lisp S-expression data-structure,
> then you should use the READ function, if it exists in Emacs-Lisp.

READ-FROM-STRING, that is.

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."