Subject: Re: Idiom to call function from string
From: (Rob Warnock)
Date: Sun, 09 Jul 2006 20:31:07 -0500
Newsgroups: comp.lang.lisp
Message-ID: <>
I just wrote:
| Sascha Wilde  <> wrote:
| +---------------
| | I want to call an existing function after construction it's name from
| | strings.  I came up with:
| | (funcall (find-symbol (string-upcase (concatenate 'string 
| |                                                   "foo-" "bar")) 
| |                       :baz))
|     (defmacro def-foo-function (name &body body)
|       `(progn (defun ,name () ,@body)
| 	      (setf (gethash ,(string-downcase (symbol-name name))
| 			     *foo-functions*)
|                     #',name)))

Oops! Sorry, I forgot that prefixing the function names with "FOO-"
was part of your spec. Please replace the above DEF-FOO-FUNCTION
with this one:

     (defmacro def-foo-function (name &body body)
       (let ((mangled-name (intern (concatenate 'string
						(symbol-name :foo-)
						(symbol-name name)))))
	 `(progn (defun ,mangled-name () ,@body)
		 (setf (gethash ,(string-downcase (symbol-name name))

[Note that the name-mangling happens only at DEF-FOO-FUNCTION time,
*not* at runtime...]

The rest of the code remains the same as before. [Well, except that
to *completely* conform to your spec, the whole file should start
with (IN-PACKAGE :BAZ).]


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607