From: Sivaram Neelakantan
Subject: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <a7043d61.0308060155.614dfd7b@posting.google.com>
Hello,

The LISP book by Winston & Horn details the usage of the above 3
primitives.   While I understand MAPCAR usage and application, quite
how the other 2 would be used beats me.  

The book says

"...use FUNCALL when you have assigned a procedure object to a
variable and you want to call that procedure object."

Why would you want to do that?  Or what exactly does this primitive
give me?

Again

"..that APPLY like FUNCALL is useful when you want to use a procedure
whose name is the value of a parameter."

confounds me.  What do these primitives make easier to do?  The
examples quoted in the book aren't clear enough to show what I can do
practically with them.

regards,
Sivaram

From: Kent M Pitman
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <sfwispb9bt7.fsf@shell01.TheWorld.com>
········@sunguru.com (Sivaram Neelakantan) writes:

> The LISP book by Winston & Horn details the usage of the above 3
> primitives.   While I understand MAPCAR usage and application, quite
> how the other 2 would be used beats me.  
> 
> The book says
> 
> "...use FUNCALL when you have assigned a procedure object to a
> variable and you want to call that procedure object."
> 
> Why would you want to do that?  Or what exactly does this primitive
> give me?
> 
> Again
> 
> "..that APPLY like FUNCALL is useful when you want to use a procedure
> whose name is the value of a parameter."
> 
> confounds me.  What do these primitives make easier to do?  The
> examples quoted in the book aren't clear enough to show what I can do
> practically with them.

Bleah.  This is just badly described in the text you quote and it's
hardly surprising you came out confused.

Both FUNCALL and APPLY are used for invoking functions that are passed
as parameters or otherwise bound to variables.  Any time you have a 
function as an object bound to a variable, these are the ways you can
invoke them.

FUNCALL is used when you can enumerate the arguments statically at
program writing time. e.g.,

 (DEFUN COMBINE3 (F X) (FUNCALL F X 3))

 (COMBINE3 #'+ 4) => 7

 (COMBINE3 #'* 4) => 12

 (COMBINE3 #'LIST 4) => (4 3)

 (COMBINE3 #'CONS 4) => (4 . 3)

APPLY is used when you cannot enumerate the arguments statically at
program writing time, so you need to pass some or all of the args
as a list.  e.g.,

 (DEFUN COMBINE7 (F &REST ARGS) (APPLY F 7 ARGS))

 (COMBINE7 #'+ 4) => 11

 (COMBINE7 #'* 4) => 28

 (COMBINE7 #'LIST 4) => (7 4)

 (COMBINE7 #'CONS 4) => (7 . 4)

 (COMBINE7 #'LIST 1 2 3) => (7 1 2 3)

 (COMBINE7 #'CONS 1 2 3) is an error [CONS got too many args, (7 1 2 3)]

MAPCAR is just a function that calls another function, so internally
it is using APPLY, as in:

 (defun my-mapcar (function &rest args)
   (let ((remaining-args (copy-list args)))
     (loop while (every (complement #'null) remaining-args)
           for function-args = (loop for args-tail on remaining-args by #'cdr
                               collect (pop (car args-tail)))
           collect (APPLY FUNCTION FUNCTION-ARGS))))

 (MY-MAPCAR #'- '(1 2 3))
 => (-1 -2 -3)

 (MY-MAPCAR #'- '(5 20 37 19) '(1 2 3))
 => (4 18 34)
From: Steven M. Haflich
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <3F311222.8040907@alum.mit.edu>
Kent M Pitman wrote:

> Both FUNCALL and APPLY are used for invoking functions that are passed
> as parameters or otherwise bound to variables.  Any time you have a 
> function as an object bound to a variable, these are the ways you can
> invoke them.

Kent, you repeat Winston and Horn's warped semantic description!

Consider code like the following:

   (funcall (gethash *command-table* char #'undefined-command-char) ...)

I haven't checked W&H to judge the confusion there, but it is common for
Lisp newbies to confuse the relation between variables and arbitrary
first-class Lisp values flowing through a computation and you should not
propagate such confusions.  A first-class function object has no
intrinsic relation with a variable that might hold it as a value, except
that a variable carries a value that is returned when that variable is
evaluated.  This is no different that the situation when the value of
a variable is a number.  Either value is no different from a value
obtained in any other way; funcall and apply know nothing special about
evaluation of variables, no more or less than car and +.
From: Kent M Pitman
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <sfwadamx1t2.fsf@shell01.TheWorld.com>
"Steven M. Haflich" <·················@alum.mit.edu> writes:

> Kent M Pitman wrote:
> 
> > Both FUNCALL and APPLY are used for invoking functions that are passed
> > as parameters or otherwise bound to variables.  Any time you have a
> > function as an object bound to a variable, these are the ways you can
> > invoke them.
> 
> Kent, you repeat Winston and Horn's warped semantic description!
> 
> Consider code like the following:
> 
>    (funcall (gethash *command-table* char #'undefined-command-char) ...)
> 
> I haven't checked W&H to judge the confusion there, but it is common for
> Lisp newbies to confuse the relation between variables and arbitrary
> first-class Lisp values flowing through a computation and you should not
> propagate such confusions.  A first-class function object has no
> intrinsic relation with a variable that might hold it as a value, except
> that a variable carries a value that is returned when that variable is
> evaluated.  This is no different that the situation when the value of
> a variable is a number.  Either value is no different from a value
> obtained in any other way; funcall and apply know nothing special about
> evaluation of variables, no more or less than car and +.

Good point.  Thanks, Steve.

For variable, read "result of a computed expression", where variables
are just trivial computations.

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.
From: Rob Warnock
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <b66cnb2hi6RCYamiXTWc-g@speakeasy.net>
Kent M 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.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Andreas Fuchs
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <86y8y2n8oh.fsf@boinkine.defun.at>
Today, Rob Warnock <····@rpw3.org> wrote:
> 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))
[...]
> 	    ...and however many
> 		 more of these you
> 		   have stomach for...
> (otherwise
> 	     (error "This implementation's CALL-ARGUMENTS-LIMIT
> 	     exceeded!"))))

Try this instead:

(defun apply-definer (max-args)
  `(defun my-apply (f &rest args)
     (case (length args)
       ,@(loop for arg-count from 0 to max-args
	   collect `((,arg-count)
		     (funcall
		      f
		      ,@(loop for arg-idx from 0 to (1- arg-count)
			   collect `(nth ,arg-idx args)))))
       (otherwise
	(error ,(format nil "Can't apply to more than ~A args" max-args))))))

(defmacro define-apply (max-args)
  (apply-definer (etypecase max-args
		   (symbol (symbol-value max-args))
		   (number max-args))))

(apply-definer call-arguments-limit)

Good that many lisps' most-positive-fixnum >= call-arguments-limit,
else we'd have to fiddle with bignums and NTH (-;

Alternatively, use the following cheat:

(defun my-apply (f &rest args)
  (multiple-value-call f (values-list args)))


Whee, reading the hyperspec is fun!
-- 
Andreas Fuchs, <···@acm.org>, ···@jabber.at, antifuchs
From: Kent M Pitman
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <sfwllu1m5nh.fsf@shell01.TheWorld.com>
Andreas Fuchs <···@void.at> writes:

> Try this instead:
> 
> (defun apply-definer (max-args)
>   `(defun my-apply (f &rest args)
>      (case (length args)
>        ,@(loop for arg-count from 0 to max-args
> 	   collect `((,arg-count)
> 		     (funcall
> 		      f
> 		      ,@(loop for arg-idx from 0 to (1- arg-count)
> 			   collect `(nth ,arg-idx args)))))
>        (otherwise
> 	(error ,(format nil "Can't apply to more than ~A args" max-args))))))
> 
> (defmacro define-apply (max-args)
>   (apply-definer (etypecase max-args
> 		   (symbol (symbol-value max-args))
> 		   (number max-args))))
> 
> (apply-definer call-arguments-limit)
> 
> Good that many lisps' most-positive-fixnum >= call-arguments-limit,
> else we'd have to fiddle with bignums and NTH (-;

Heh.  MACSYMA used Maclisp functions FILLARRAY and LISTARRAY which
would linearly list the contents of any array (in row major order),
regardless of arity.  When I translated MACSYMA to Common Lisp, I
didn't have ROW-MAJOR-AREF, so I did a scheme like this with
ARRAY-RANK-LIMIT.  Most implementations have a limit of about 7 or 8.
Imagine my surprise when some implementations had enormous array rank
limits and this ran out of control.  Further imagine my surprise when
7 nested do loops took (I think) exponentially long to compile due to
macroexpanding the first form to see if it had declarations.  Both of
these problems led to proposals (the addition of ROW-MAJOR-AREF and
the doing away with macroexpansion of initial forms in a body to find
declarations).  In any case, I don't do this kind of "cute trick" as
much any more.
From: Rob Warnock
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <XrmdndhOAr8S_qqiXTWc-g@speakeasy.net>
Andreas Fuchs  <···@void.at> wrote:
+---------------
| Today, Rob Warnock <····@rpw3.org> wrote:
| > 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 ...
...
| Try this instead:
| 
| (defun apply-definer (max-args)
|   `(defun my-apply (f &rest args)
|      (case (length args)
|        ,@(loop for arg-count from 0 to max-args
| 	   collect `((,arg-count)
| 		     (funcall
| 		      f
| 		      ,@(loop for arg-idx from 0 to (1- arg-count)
| 			   collect `(nth ,arg-idx args)))))
|        (otherwise
| 	(error ,(format nil "Can't apply to more than ~A args" max-args))))))
+---------------

D'oh!! Just when I begin to think I'm finally starting to think in CL,
somebody like you comes along and points out that I really haven't yet
fully internalized "the Lisp way"[1].

Thanks. (No, really!!)


-Rob

[1] Not to be confused with The Lambda Nature, which even Schemers have
    to some extent.

-----
Rob Warnock, PP-ASEL-IA		<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Pascal Costanza
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <costanza-AEC3BD.19464009082003@news.netcologne.de>
In article <······················@speakeasy.net>,
 ····@rpw3.org (Rob Warnock) wrote:

> 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.

What's wrong with the following definition?

(defun apply (f arglist)
  (eval `(funcall ,f ,@arglist)))



Pascal
From: Paul F. Dietz
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <MKqcnR0scrFRq6iiU-KYvA@dls.net>
Pascal Costanza wrote:

> What's wrong with the following definition?
> 
> (defun apply (f arglist)
>   (eval `(funcall ,f ,@arglist)))

It evaluates f and the elements of arglist again.  You mean:

(defun apply (f arglist)
   (eval `(funcall (quote ,f)
		  ,@(mapcar #'(lambda (arg) `(quote ,arg))
			 arglist))))

	Paul
From: Jeremy Yallop
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <bh3gg4$ul8hc$1@ID-114079.news.uni-berlin.de>
Paul F. Dietz wrote:
> Pascal Costanza wrote:
> 
>> What's wrong with the following definition?
>> 
>> (defun apply (f arglist)
>>   (eval `(funcall ,f ,@arglist)))
> 
> It evaluates f and the elements of arglist again.  You mean:
> 
> (defun apply (f arglist)
>    (eval `(funcall (quote ,f)
> 		  ,@(mapcar #'(lambda (arg) `(quote ,arg))
> 			 arglist))))

That doesn't handle the case where there are more than two arguments, e.g.

   (apply #'+ 3 4 '(5))

Jeremy.
From: Pascal Costanza
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <costanza-9A8544.21182709082003@news.netcologne.de>
In article <··············@ID-114079.news.uni-berlin.de>,
 Jeremy Yallop <······@jdyallop.freeserve.co.uk> wrote:

> Paul F. Dietz wrote:
> > Pascal Costanza wrote:
> > 
> >> What's wrong with the following definition?
> >> 
> >> (defun apply (f arglist)
> >>   (eval `(funcall ,f ,@arglist)))
> > 
> > It evaluates f and the elements of arglist again.  You mean:
> > 
> > (defun apply (f arglist)
> >    (eval `(funcall (quote ,f)
> > 		  ,@(mapcar #'(lambda (arg) `(quote ,arg))
> > 			 arglist))))
> 
> That doesn't handle the case where there are more than two arguments, e.g.
> 
>    (apply #'+ 3 4 '(5))


OK, here's a new suggestion:

(defun apply (f &rest arglist)
  (eval
   `(funcall
     ',f 
     ,@(let ((args nil)
             (argl (reverse arglist)))
         (dolist (arg (car argl))
           (push `',arg args))
         (dolist (arg (cdr argl))
           (push `',arg args))
         args))))

Would it be ok to say nreverse instead of reverse?

How good/bad is this code wrt efficiency?


Pascal
From: Edi Weitz
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <87fzka8vjr.fsf@bird.agharta.de>
On Sat, 09 Aug 2003 21:18:27 +0200, Pascal Costanza <········@web.de> wrote:

> OK, here's a new suggestion:
> 
> (defun apply (f &rest arglist)
>   (eval
>    `(funcall
>      ',f 
>      ,@(let ((args nil)
>              (argl (reverse arglist)))
>          (dolist (arg (car argl))
>            (push `',arg args))
>          (dolist (arg (cdr argl))
>            (push `',arg args))
>          args))))
> 
> Would it be ok to say nreverse instead of reverse?

<http://www.google.com/groups?selm=3247672165664225%40naggum.no>

Edi.
From: Pascal Costanza
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <costanza-B44065.00370210082003@news.netcologne.de>
In article <··············@bird.agharta.de>, Edi Weitz <···@agharta.de> 
wrote:

> > Would it be ok to say nreverse instead of reverse?
> 
> <http://www.google.com/groups?selm=3247672165664225%40naggum.no>
> 

Ah, ok. Thanks.


Pascal
From: Steven M. Haflich
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <vt8Ya.541$Pf7.46590223@newssvr13.news.prodigy.com>
Steven M. Haflich wrote:

>   (funcall (gethash *command-table* char #'undefined-command-char) ...)

Oof!  I wrote the arguments to gethash backward

(funcall (gethash char *command-table* #'undefined-command-char) ...)

The point, of course, was that the function being called is never the
value of any variable.
From: Kent M Pitman
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <sfw65lax1qo.fsf@shell01.TheWorld.com>
"Steven M. Haflich" <·················@alum.mit.edu> writes:

> Steven M. Haflich wrote:
> 
> >   (funcall (gethash *command-table* char #'undefined-command-char) ...)
> 
> Oof!  I wrote the arguments to gethash backward
> 
> (funcall (gethash char *command-table* #'undefined-command-char) ...)
> 
> The point, of course, was that the function being called is never the
> value of any variable.

Heh.  I was just assuming that *command-table* contained a character
and that char contained a hash table.  But you're right, it's even clearer
when you name the variables in a way that's compatible with the types
they will carry... ;)

[That must be why I like Lisp2 so much.  I don't want to find that type
 names named globally are unavailable to me as variable names...]
From: Joe Marshall
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <vfta517w.fsf@ccs.neu.edu>
········@sunguru.com (Sivaram Neelakantan) writes:

> Hello,
>
> The LISP book by Winston & Horn details the usage of the above 3
> primitives.   While I understand MAPCAR usage and application, quite
> how the other 2 would be used beats me.  
>
> The book says
>
> "...use FUNCALL when you have assigned a procedure object to a
> variable and you want to call that procedure object."
>
> Why would you want to do that?  Or what exactly does this primitive
> give me?

Suppose you want to write a program that works like mapcar.

(defun my-program (f list)
    ...
    ... now we want to call the function F ...
    ...)

At some point, you want to invoke that function that the user passed
in.  To do that you write (funcall f ...arguments go here....)

>
> "..that APPLY like FUNCALL is useful when you want to use a procedure
> whose name is the value of a parameter."
>
> confounds me.  What do these primitives make easier to do?  The
> examples quoted in the book aren't clear enough to show what I can do
> practically with them.

Just ignore APPLY.  At some point you will be writing some code like
the above, but you'll have a list of arguments rather than a fixed
number of arguments.  FUNCALL won't work because your function is
expecting the elements within the list, not the list itself.  As soon
as you run into this, the whole point of APPLY will be obvious.
From: Kent M Pitman
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <sfw1xvyx1oh.fsf@shell01.TheWorld.com>
Joe Marshall <···@ccs.neu.edu> writes:

> Just ignore APPLY.  At some point you will be writing some code like
> the above, but you'll have a list of arguments rather than a fixed
> number of arguments.  FUNCALL won't work because your function is
> expecting the elements within the list, not the list itself.  As soon
> as you run into this, the whole point of APPLY will be obvious.

Well, strictly speaking, you _can_ still use FUNCALL in this 
case--you just have to APPLY it. ;)
From: Sivaram Neelakantan
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <a7043d61.0308062022.58c1918d@posting.google.com>
Joe Marshall <···@ccs.neu.edu> wrote in message 

[...]
> Just ignore APPLY.  <snip>

The best advice I received so far. :)  Ignore it till the problem goes
away or that the problem requires the use of it.

> the above, but you'll have a list of arguments rather than a fixed
> number of arguments.  FUNCALL won't work because your function is
> expecting the elements within the list, not the list itself.  As soon
> as you run into this, the whole point of APPLY will be obvious.

I hope so, I'm having so many "arrgh, I don't grok this" moments that
that day is someday off.

regards,
Sivaram
From: Alex Drummond
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <bgqjrg$ak7$1@newsg3.svr.pol.co.uk>
Well, take a look at a simple definition of mapcar. It takes two arguments,
a function to map over a list, and the list itself:

    (defun my-mapcar (func list)
       ...more Lisp code here...)

When we want to call the function passed as the first argument, we have to
use funcall (or apply):

    ;;; simple recursive defintion of mapcar
    (defun mapcar (func list)
      (cons (funcall func (car list))
            (mapcar func (cdr list))))

So you need to use funcall/apply whenever you're writing a higher order
function (i.e. a function which takes another function as its argument).

Alex

Sivaram Neelakantan wrote:

> Hello,
> 
> The LISP book by Winston & Horn details the usage of the above 3
> primitives.   While I understand MAPCAR usage and application, quite
> how the other 2 would be used beats me.
> 
> The book says
> 
> "...use FUNCALL when you have assigned a procedure object to a
> variable and you want to call that procedure object."
> 
> Why would you want to do that?  Or what exactly does this primitive
> give me?
> 
> Again
> 
> "..that APPLY like FUNCALL is useful when you want to use a procedure
> whose name is the value of a parameter."
> 
> confounds me.  What do these primitives make easier to do?  The
> examples quoted in the book aren't clear enough to show what I can do
> practically with them.
> 
> regards,
> Sivaram
From: Alex Drummond
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <bgqk8c$k2t$1@news7.svr.pol.co.uk>
Alex Drummond wrote:

Hmm, my definition of mapcar was a bit dodgy there, here's a better one:

    (defun my-mapcar (func list)
      (if (null list)
        nil
        (cons (funcall func (car list))
              (mapcar func (cdr list)))))

Alex

> Well, take a look at a simple definition of mapcar. It takes two
> arguments, a function to map over a list, and the list itself:
> 
>     (defun my-mapcar (func list)
>        ...more Lisp code here...)
> 
> When we want to call the function passed as the first argument, we have to
> use funcall (or apply):
> 
>     ;;; simple recursive defintion of mapcar
>     (defun mapcar (func list)
>       (cons (funcall func (car list))
>             (mapcar func (cdr list))))
> 
> So you need to use funcall/apply whenever you're writing a higher order
> function (i.e. a function which takes another function as its argument).
> 
> Alex
> 
> Sivaram Neelakantan wrote:
> 
>> Hello,
>> 
>> The LISP book by Winston & Horn details the usage of the above 3
>> primitives.   While I understand MAPCAR usage and application, quite
>> how the other 2 would be used beats me.
>> 
>> The book says
>> 
>> "...use FUNCALL when you have assigned a procedure object to a
>> variable and you want to call that procedure object."
>> 
>> Why would you want to do that?  Or what exactly does this primitive
>> give me?
>> 
>> Again
>> 
>> "..that APPLY like FUNCALL is useful when you want to use a procedure
>> whose name is the value of a parameter."
>> 
>> confounds me.  What do these primitives make easier to do?  The
>> examples quoted in the book aren't clear enough to show what I can do
>> practically with them.
>> 
>> regards,
>> Sivaram
From: Coby Beck
Subject: Re: MAPCAR, FUNCALL & APPLY usage
Date: 
Message-ID: <bgqk84$29me$1@otis.netspace.net.au>
"Sivaram Neelakantan" <········@sunguru.com> wrote in message
·································@posting.google.com...
> Hello,
>
> The LISP book by Winston & Horn details the usage of the above 3
> primitives.   While I understand MAPCAR usage and application, quite
> how the other 2 would be used beats me.
>
> The book says
>
> "...use FUNCALL when you have assigned a procedure object to a
> variable and you want to call that procedure object."
>
> Why would you want to do that?  Or what exactly does this primitive
> give me?
>
> Again
>
> "..that APPLY like FUNCALL is useful when you want to use a procedure
> whose name is the value of a parameter."
>
> confounds me.  What do these primitives make easier to do?  The
> examples quoted in the book aren't clear enough to show what I can do
> practically with them.
>

CL-USER 25 > (defparameter *operator* #'+)
*OPERATOR*

CL-USER 26 > (defparameter *converter* #'parse-integer)
*CONVERTER*

CL-USER 27 > (defparameter *data* (list "5" "6" "7"))
*DATA*

CL-USER 28 > (apply *operator* (loop for datum in *data*
                                    collect (funcall *converter* datum)))
18

CL-USER 29 > (setf *operator* #'*)
#<function * 20119FD2>

CL-USER 30 > (apply *operator* (loop for datum in *data*
                                    collect (funcall *converter* datum)))
210

CL-USER 31 > (setf *converter* #'(lambda (str)
                                   (* 10 (parse-integer str))))
#'(LAMBDA (STR) (* 10 (PARSE-INTEGER STR)))

CL-USER 32 > (apply *operator* (loop for datum in *data*
                                    collect (funcall *converter* datum)))
210000

And other wonderful stuff.
HTH...

-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")