This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Calling a returned function.

5 posts · 1437 views

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.)
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 , edited 2 times in total.

I'm off my grokker.
- Chris

Re: Calling a returned function.

You need funcall:
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

Re: Calling a returned function.

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

Re: Calling a returned function.

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.

Re: Calling a returned function.

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