another blast from the past
From: "Vladimir Nesterovsky"
Subject: Re: Mapcar mistery
Date: Wednesday, November 07, 2001 8:57 PM
J. Avelar wrote in message
news:42BE6B74CE9E5E79D7798C80DC372C8A@in.WebX.maYIadrTaRb...
> Hi Doug,
>
> Thanks for the reply, but I still have some doubts.
>
> I typed (cons 'list table) and it returned something slightly different
from
> ('list (a b c d) (1 2 3 4)), it returned (LIST (a b c d) (1 2 3 4)). Note
> the absence of '.
>
> Sorry I still don't get he difference between apply 'mapcar and just
> mapcar. Doesn't mapcar performs a function to a list?
>
> In this case, is LIST the function it performs on the list?
>
EXACTLY. Mapcar expects a symbol which must be a function's name.
It interprets it as such and applies the function on arguments it's supplied
in a list. So the symbol LIST is used as a function name here. When it is
printed, it is printed as LIST. Would we type it like (list 1 2 3), it would
be
interpreted as a function call by Autolisp system. If lst is '(1 2 3),
(cons 'list lst) will produce (LIST 1 2 3), etc...
Apply feeds each element in a list as separate function argument to a
function
it is called with, it kind of "opens the parentheses". So when we write
(apply 'mapcar (cons 'list tbl)) it's equivalent to
(mapcar 'list (car tbl) (cadr tbl) (nth 2 tbl) (nth 3 tbl) ...... (last
tbl) )
Apply doesn't care that the symbol 'LIST names a function, all it sees
is a list of symbol LIST and then some lists (top elements of table which is
a list of lists). Then, when mapcar goes into action, it sees the symbol
LIST
and uses it as a name of a function, namely, the function LIST.
What makes such code possible, is that in Lisp, code is data, and data is
code.
Happy lisping,
--
Have fun, 🙂
Vlad http://vnestr.tripod.com/
(define (average . args)
(if args (/ (apply '+ args) (length args))))