Lisp HUG Maillist Archive

Converting a string name into a function call

I have defined several functions with no arguments, for example:
 
(defun foo () etc)
 
(defun bar () etc)
 
etc
 
My program generates function names as strings like this:
 
"FOO"
 
When my program gives me the name "FOO", I'd like to then call and execute the corresponding function (FOO).  Is there a way to convert the string name into a callable function name?  I can intern the string, but that apparently doesn't bind it to the desired function.  (I was hoping to avoid a bunch of IF-THEN look-ups.)
 
Bruce.
 
 
 

Re: Converting a string name into a function call


On Jan 17, 2008, at 17:02 , Bruce J Weimer MD wrote:

> I have defined several functions with no arguments, for example:
>
> (defun foo () etc)
>
> (defun bar () etc)
>

In which file and package?


> etc
>
> My program generates function names as strings like this:
>
> "FOO"

Which program?

>
> When my program gives me the name "FOO", I'd like to then call and  
> execute the corresponding function (FOO).  Is there a way to  
> convert the string name into a callable function name?  I can  
> intern the string, but that apparently doesn't bind it to the  
> desired function.  (I was hoping to avoid a bunch of IF-THEN look- 
> ups.)

Interning seems the right thing.  You are probably missing the right  
package.

Cheers

Marco





Re: Converting a string name into a function call

Bruce J Weimer MD wrote:

> When my program gives me the name "FOO", I'd like to then call and
> execute the corresponding function (FOO).  Is there a way to convert
> the string name into a callable function name?  I can intern the
> string, but that apparently doesn't bind it to the desired function.

That sounds a bit strange.  My guess is that you tried to call the
function in the wrong way, or you have a package mismatch (when your
function is defined in one package and you're interning your string
in another package).

Something like the following should work, I think:

(defun find-and-call-function-in-package (string package)
   (let ((function-name (find-symbol string package)))
     ;; Check that the symbol exists.
     (when (null function-name)
       (error "~S does not exist in package ~S" string package))
     ;; Check that the symbol names a function.
     (unless (fboundp function-name)
       (error "~S is not fbound" function-name))
     ;; Everything looks fine: call the function.
     (funcall function-name)))

If your program gets this function name 'from outside', you'd
better make very sure that you're not funcalling the function
FORMAT-MY-HARD-DRIVE-AND-SEND-SOME-MISSILES-TO-CHINA...

Arthur



Re: Converting a string name into a function call

"Bruce J Weimer MD" <bjweimer@charter.net> writes:

> I have defined several functions with no arguments, for example:
>  
> (defun foo () etc)
>  
> (defun bar () etc)
>  
> etc
>  
> My program generates function names as strings like this:
>  
> "FOO"
>  
> When my program gives me the name "FOO", I'd like to then call and execute the corresponding function (FOO).  Is there a way to convert the string name into a callable function name?  I
> can intern the string, but that apparently doesn't bind it to the desired function.  (I was hoping to avoid a bunch of IF-THEN look-ups.)

Works for me:

CL-USER 36 > (defun bar ()
               "bar")
BAR

CL-USER 37 > (funcall (intern "BAR"))
"bar"

You probably defined the functions and interned the symbols in different packages.

Nico


Re: Converting a string name into a function call

Does 

(funcall (symbol-function (intern "FOO")) etc)

do what you want?

On Jan 17, 2008, at 17:02 , Bruce J Weimer MD wrote:

I have defined several functions with no arguments, for example:
 
(defun foo () etc)
 
(defun bar () etc)
 
etc
 
My program generates function names as strings like this:
 
"FOO"
 
When my program gives me the name "FOO", I'd like to then call and execute the corresponding function (FOO).  Is there a way to convert the string name into a callable function name?  I can intern the string, but that apparently doesn't bind it to the desired function.  (I was hoping to avoid a bunch of IF-THEN look-ups.)
 
Bruce.
 
 
 

Re: Converting a string name into a function call

CL-USER> (defun foo () 'etc)
FOO
CL-USER> (funcall (intern "FOO"))
ETC

Does that help?

On Jan 17, 2008 8:02 AM, Bruce J Weimer MD <bjweimer@charter.net> wrote:
>
>
> I have defined several functions with no arguments, for example:
>
> (defun foo () etc)
>
>
> (defun bar () etc)
>
> etc
>
> My program generates function names as strings like this:
>
> "FOO"
>
> When my program gives me the name "FOO", I'd like to then call and execute
> the corresponding function (FOO).  Is there a way to convert the string name
> into a callable function name?  I can intern the string, but that apparently
> doesn't bind it to the desired function.  (I was hoping to avoid a bunch of
> IF-THEN look-ups.)
>
> Bruce.
>
>
>


Updated at: 2020-12-10 08:44 UTC