Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Executing a function stored in a symbol.

5 REPLIES 5
Reply
Message 1 of 6
sunburned.surveyor
316 Views, 5 Replies

Executing a function stored in a symbol.

I've been reading the forum and a couple of my AutoLISP books for the past hour. I can't figure out how to execute a function stored in a symbol.

Consider the following function:

(defun modnum (num) (+ num 1))

Then this statement which stores the function in a symbol:

(setq somefunc modnum)

Now I want to execute modnum from the symbol. I can get this to work by storing the function definition for modnum in a string and then using the eval/read combo, but there has to be a way to avoid putting the function definition in a String symbol. I want to just execute a function stored in the symbol directly.

Any suggesitons?

Thanks,

The Sunburned Surveyor
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: sunburned.surveyor

(eval (list (read somefunc)))

wrote in message
news:6361869@discussion.autodesk.com...
I've been reading the forum and a couple of my AutoLISP books for the past
hour. I can't figure out how to execute a function stored in a symbol.

Consider the following function:

(defun modnum (num) (+ num 1))

Then this statement which stores the function in a symbol:

(setq somefunc modnum)

Now I want to execute modnum from the symbol. I can get this to work by
storing the function definition for modnum in a string and then using the
eval/read combo, but there has to be a way to avoid putting the function
definition in a String symbol. I want to just execute a function stored in
the symbol directly.

Any suggesitons?

Thanks,

The Sunburned Surveyor
Message 3 of 6

Turns out I needed to do something like this:

(defun modnum (num) (+ num 1))

(setq somefunc modnum)

(eval '(somefunc 1))

I think I was missing the fact that I needed to escape the function call to eval with a quote. No need for Strings!

The Sunburned Surveyor
Message 4 of 6

Hi,

Even this is unnecessarily complicated. The ordinary Lisp function call semantics take care of this:


_$ (defun modnum (num) (+ num 1))
MODNUM
_$ (setq somefunc modnum)
#
_$ (somefunc 7)
8

In other words, you don't need an explicit EVAL call.

In this regard AutoLISP is a so-called Lisp-1, where the functions and other values are in the same namespace.
The best-known language variant in this family is Scheme.

The other possibility is Lisp-2, where functions are in another namespace, like in Common Lisp.
There you need a slightly more complicated syntax for this:

(setq somefunc #'modnum)

(funcall somefunc 7)


Generally, there are very few places where you need an explicit EVAlL call, most of the time there are better ways of doing things.

--
Message 5 of 6

Thanks for the response Martti. I acutally need to store the function in a symbol, because I won't be able to determine the function's name at run time. I'm trying to use something like a function pointer in C programming to emulate an interface in AutoLISP. The idea being I obtain the function pointer from a list and then execute it, with no knowledge of the function implementation at run time, other than the parameter list and return type.

I'll post to the list with the results of my experiment.
Message 6 of 6

If you want to know how this should be done, take a look here:

http://mitpress.mit.edu/sicp/


- That is a Scheme book (available online), so there are some differences from AutoLISP, but the mechanism for storing and calling functions as values is the same.

- One thing that might confuse a casual reader is slightly different function definition forms:

AutoLISP:

(defun my-func (a b c) ...)

Scheme:

(define (my-func a b c) ...)


--

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost