Subject: Re: MAPCAR, FUNCALL & APPLY usage
From: rpw3@rpw3.org (Rob Warnock)
Date: Sat, 09 Aug 2003 08:53:35 -0500
Newsgroups: comp.lang.lisp
Message-ID: <b66cnb2hi6RCYamiXTWc-g@speakeasy.net>
Kent M Pitman  <pitman@world.std.com> wrote:
+---------------
| I was focusing on a completely different conceptual bug which was the
| lack of symmetry in how FUNCALL and APPLY are described.  That is, that
| they do the same thing and differ only in how they take their args.
+---------------

To me [when coming to CL from Scheme], it was important to note that
APPLY is definitely the more fundamental of the two. FUNCALL can be
trivially defined in terms of APPLY like so:

	(defun funcall (f &rest rest)
	  (apply f rest))

but there is no straightforward corresponding inverse case.

Well, let me qualify that: If you do try to implement APPLY in terms of
FUNCALL, you end up with the same kind of godawful mess you get when
trying to emulate APPLY in C [whose function call indirect through a
pointer has exactly same limitation as FUNCALL]. That is, about the
closest you can get to an inverse of the above is:

	(defun apply (f args)	; ignoring for now the general signature
	  (case (length args)
	    ((0) (funcall f))
	    ((1) (funcall f (first args)))
	    ((2) (funcall f (first args) (second args)))
	    ((3) (funcall f (first args) (second args) (third args)))
	    ((4) (funcall f (first args) (second args) (third args)
			    (fourth args)))
	    ((5) (funcall f (first args) (second args) (third args)
			    (fourth args) (fifth args)))
	    ((6) (funcall f (first args) (second args) (third args)
			    (fourth args) (fifth args) (sixth args)))
	    ...and however many
		 more of these you
		   have stomach for...
            (otherwise
	     (error "This implementation's CALL-ARGUMENTS-LIMIT exceeded!"))))


-Rob

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