Calling a returned function.

Discussion of Common Lisp
Post Reply
Jesdisciple
Posts: 26
Joined: Thu Feb 04, 2010 5:56 pm

Calling a returned function.

Post by Jesdisciple » Mon Feb 08, 2010 9:03 am

The details of why I'm trying to do this are somewhat complicated, and I know how I can workaround the problem, but why can't I make an inline call to a function returned from another function? I can't find any punctuation things (' #' @, , `) that can help. Here's a test case...

(BTW, if there's a name for the syntactic sugar that's not really punctuation, I would appreciate it.)

Code: Select all

CL-USER> (defun foo () #'bar)
FOO
CL-USER> (defun bar () (* 70 7))
BAR
CL-USER> (foo)
#<FUNCTION BAR NIL (DECLARE (SYSTEM::IN-DEFUN BAR)) (BLOCK BAR (* 70 7))>
CL-USER> ((foo))
EVAL: #1=(FOO) is not a function name; try using a symbol instead
   [Condition of type SYSTEM::SIMPLE-SOURCE-PROGRAM-ERROR]
; Evaluation aborted.
CL-USER> (setf (fdefinition `???) (foo))
#<FUNCTION BAR NIL (DECLARE (SYSTEM::IN-DEFUN BAR)) (BLOCK BAR (* 70 7))>
CL-USER> (???)
490
Last edited by Jesdisciple on Mon Feb 08, 2010 9:40 am, edited 2 times in total.
I'm off my grokker.
- Chris

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Calling a returned function.

Post by gugamilare » Mon Feb 08, 2010 9:10 am

You need funcall:

Code: Select all

cl-user> (defun bar () 490)
bar
cl-user> (defun foo () #'bar)
style-warning: redefining foo in DEFUN
foo
cl-user> 
cl-user> (defun bar () 490)
bar
cl-user> (defun foo () #'bar)
foo
cl-user> (funcall (foo))
490

Jesdisciple
Posts: 26
Joined: Thu Feb 04, 2010 5:56 pm

Re: Calling a returned function.

Post by Jesdisciple » Mon Feb 08, 2010 9:46 am

Ah, thanks. But wow, that might make my program really verbose.

I am still curious about the punctuation though, partly because I wonder whether I could define my own...
I'm off my grokker.
- Chris

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Calling a returned function.

Post by ramarren » Mon Feb 08, 2010 10:12 am

Jesdisciple wrote:Ah, thanks. But wow, that might make my program really verbose.
That is not that likely unless your have a very wide definition of "really verbose". I suppose there are some problems which involve lots of calling of value-space functions, but I don't think there are a lot, and if anything adding FUNCALL/APPLY expresses intent better and so is more readable.
Jesdisciple wrote:I am still curious about the punctuation though, partly because I wonder whether I could define my own...
You can define your own syntax sugar, Common Lisp calls those "read macros". But it is usually not that good of an idea, since syntax doesn't compose well, and it confuses Lisp aware editors. Anyway, On Lisp has a chapter on read macros.

Jesdisciple
Posts: 26
Joined: Thu Feb 04, 2010 5:56 pm

Re: Calling a returned function.

Post by Jesdisciple » Mon Feb 08, 2010 11:08 pm

I can see the readability going either way... I guess I'll wait until my functions work to decide, but as it stands I suspect I'll end up with Scheme unless I just can't part with CL macros once I learn them. On the bright side, I'm pretty sure (a) Lisp will become one of my favorite languages.

Thanks for the term... To be honest I doubt I'll define my own because they're some of my least-favorite syntax(-ish) elements. But that depends on the use I find and how pretty a prefix I can use.
I'm off my grokker.
- Chris

Post Reply