From: Josef Eschgfaeller
Subject: funcall
Date: 
Message-ID: <Pine.LNX.4.04.9906152313370.12148-100000@arbzi.zuhause.fe>
Graham and Touretzky in their books seem to prefer using funcall
for functions which appear as arguments. Is

  (defun augm (f x) (+ x (funcall f x)))

more correct than

  (defun augm (f x) (+ x (f x)))?

Since with the latter (augm #'f 5) works also with

  (setf (symbol-function 'f) #'(lambda (x) (+ x x)))

and

  (setf f #'(lambda (x) (+ x x))),

why do those authors use funcall in these cases?
And what is exactly the difference between the last two setfs?

Josef Eschgfaeller
From: Barry Margolin
Subject: Re: funcall
Date: 
Message-ID: <DJy93.651$KM3.198994@burlma1-snr2>
In article <········································@arbzi.zuhause.fe>,
Josef Eschgfaeller  <···@felix.unife.it> wrote:
>
>Graham and Touretzky in their books seem to prefer using funcall
>for functions which appear as arguments. Is
>
>  (defun augm (f x) (+ x (funcall f x)))
>
>more correct than
>
>  (defun augm (f x) (+ x (f x)))?

Yes, because the latter won't work in Common Lisp.  In Common Lisp (and
many other Lisp dialects) there's a separate namespace for function and
variable bindings.  When AUGM is called, F is bound in the variable
namespace, but (+ x (f x)) refers to F's function binding.

Your second version will work in Scheme (after changing the syntax to be
appropriate for "define") because it uses the same namespace for functions
and variables.  Scheme doesn't have or need FUNCALL because of this.

>Since with the latter (augm #'f 5) works also with
>
>  (setf (symbol-function 'f) #'(lambda (x) (+ x x)))

But note that this will have the same effect if AUGM were written as:

(defun augm (not-f x) (+ x (f x)))

The call to F in (f x) is not really referring to the same thing that's
being bound in the parameter list.  It's calling the global function F,
which is what you assign with the SETF.

>
>and
>
>  (setf f #'(lambda (x) (+ x x))),
>
>why do those authors use funcall in these cases?

This one will *not* work.  Maybe it worked for you because you never undid
the first setf.

>And what is exactly the difference between the last two setfs?

The first one sets F's global function binding, the second one sets its
variable binding.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.