Subject: Re: #' and lambda
From: rpw3@rpw3.org (Rob Warnock)
Date: Sat, 11 Feb 2006 23:16:33 -0600
Newsgroups: comp.lang.lisp
Message-ID: <5OednehH6r0sWXPenZ2dnUVZ_tOdnZ2d@speakeasy.net>
Coby Beck <cbeck@mercury.bc.ca> wrote:
+---------------
| I also recall getting different behaviour from #'whoosh and 'whoosh when 
| there was only a global function binding.  IIRC, I asked about it here and 
| Kent Pitman among others discussed it with me (summer/fall 2001). I *think* 
| this was conforming behaviour (LW 4) though not required and by consensus 
| not desired:  in a (mapcar #'whoosh long-list) where whoosh was redefined 
| during the iteration the new definition was never used...
+---------------

This is *required* behavior! MAPCAR is a function, and Common Lisp
is call-by-value, so the argument is evaluated *once*, produces a
function object, and that [which is constant, immutable] is passed
to MAPCAR, just as if you had said this:

    (let ((temp #'whoosh))
      (mapcar temp long-list))

Changing the function binding of WHOOSH during the call can have no
possible effect on the constant/immutable function object.

+---------------
| ...whereas if I used 'whoosh the new definition was used as soon
| as available.
+---------------

That behavior is the one which I believe is optional. In this case
what you are passing to MAPCAR is a symbol, not a function, and
all CLHS "MAPCAR" says is:

    If function is a symbol, it is coerced to a function as if by
    SYMBOL-FUNCTION.

It leaves unspecified whether that coersion occurs just once upon
entry to MAPCAR or repeatedly before each step of the list traversal.

+---------------
| Presumably an optimisation, retrieving the function was and using
| it many time vs calling (symbol-function 'whoosh) every time before
| using it. 
+---------------

Yup. Note that unless APPLY is inlined within MAPCAR (and friends),
it is likely that in most implememntations the function-designator
argument will simply be passed on to APPLY unmodified, and thus APPLY
will do any needed SYMBOL-FUNCTION coercion for each list item.
[This is the case in CLISP & CMUCL, at least.]

But AFAICT there is nothing in the CLHS *forbidding* the optimization
of calling SYMBOL-FUNCTION only once in MAPCAR...


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607