From: Stuart Hirons
Subject: use of mapcar
Date: 
Message-ID: <864200900.1444.0.nnrp-5.9e98cab4@news.demon.co.uk>
Hello, trying to use ' mapcar' on a list and call a function with
each of the items in the list, plus 2 other params which remain
constant throughout.

i.e.

(defun myfunc (a b c)
   (cond
      .....various guards etc go here...
     (t (mapcar 'myfunc '(cat dog lion) b c))))

so what it *should* do is recursively call itself with

myfunc (cat b c)
.then
    myfunc (dog b c)
    then.....
      myfunc (lion b c)

but of course it doesn't as I have the syntax wrong.

Can anyone give me any pointers ??



Stuart Hirons, Nottingham, England.

From: Barry Margolin
Subject: Re: use of mapcar
Date: 
Message-ID: <5lugt0$b0m@pasilla.bbnplanet.com>
In article <································@news.demon.co.uk>,
Stuart Hirons <······@brussel.demon.co.uk> wrote:
>Hello, trying to use ' mapcar' on a list and call a function with
>each of the items in the list, plus 2 other params which remain
>constant throughout.

>(defun myfunc (a b c)
>   (cond
>      .....various guards etc go here...
>     (t (mapcar 'myfunc '(cat dog lion) b c))))

That clearly won't work.  The description of MAPCAR says that it expects
all the arguments (except the function, of course) to be lists; it calls
the function first with the first element of each list, then with the
second element of each list, and so on.

If you want to use one list and a bunch of singletons, you need a function
that takes one argument and calls the function with that argument and the
singletons.  To do this, the last line of your function above should be:

  (t (mapcar #'(lambda (x) (myfunc x b c))
             '(cat dog lion)))
-- 
Barry Margolin, ······@bbnplanet.com
BBN Corporation, Cambridge, MA
(BBN customers, call (800) 632-7638 option 1 for support)
Support the anti-spam movement; see <http://www.cauce.org/>
From: Mark McConnell
Subject: Re: use of mapcar
Date: 
Message-ID: <338B3040.7063@math.okstate.edu>
Stuart Hirons wrote:
> 
> Hello, trying to use ' mapcar' on a list and call a function with
> each of the items in the list, plus 2 other params which remain
> constant throughout.
> 
> i.e.
> 
> (defun myfunc (a b c)
>    (cond
>       .....various guards etc go here...
>      (t (mapcar 'myfunc '(cat dog lion) b c))))
> 
> so what it *should* do is recursively call itself with
> 
> myfunc (cat b c)
> .then
>     myfunc (dog b c)
>     then.....
>       myfunc (lion b c)
> 
> but of course it doesn't as I have the syntax wrong.

First, use (mapcar #'myfunc ...) instead of (mapcar 'myfunc ...)

mapcar needs all its arguments (not counting the first one, the
function) to be lists.  The b and c can't be constants, the way
you're using it.  Perhaps you're confusing it with other languages
(e.g. Maple).

And if you try to do
(mapcar #'myfunc '(cat dog lion) (list b) (list c))
you'll only get a list with the single element (myfunc cat b c),
since (list b) and (list c) have only one element and the mapcar
will "run out" after that.

I think you want, in the last clause after the t,

(mapcar #'(lambda (x) (myfunc x b c))
        '(cat dog lion))
From: Dorai Sitaram
Subject: Re: use of mapcar
Date: 
Message-ID: <5mpdkn$rmp@news.gte.com>
In article <·············@math.okstate.edu>,
Mark McConnell  <·······@math.okstate.edu> wrote:
>First, use (mapcar #'myfunc ...) instead of (mapcar 'myfunc ...)

I'm curious why many stylists suggest this.  'myfunc, of
course, works just as well as #'myfunc.  If Common Lisp is
offering to distinguish between symbol-function and
symbol-value, why not use it?

My concern is not so much with saving one keystroke.
Rather, I have noticed that when compiling Lisp files that
have a funcall to a function textually before it is defun'd,
#'myfunc signals a useless warning, whereas 'myfunc doesn't.

--d
-- 
http://www.cs.rice.edu/~dorai
From: Hrvoje Niksic
Subject: Re: use of mapcar
Date: 
Message-ID: <kigaflbll54.fsf@jagor.srce.hr>
····@bunny.gte.com (Dorai Sitaram) writes:

> In article <·············@math.okstate.edu>,
> Mark McConnell  <·······@math.okstate.edu> wrote:
> >First, use (mapcar #'myfunc ...) instead of (mapcar 'myfunc ...)
> 
> I'm curious why many stylists suggest this.  'myfunc, of
> course, works just as well as #'myfunc.  If Common Lisp is
> offering to distinguish between symbol-function and
> symbol-value, why not use it?

A reason I've heard is that #'myfunc is looked up in the current
package, whereas 'myfunc only globally.

-- 
Hrvoje Niksic <·······@srce.hr> | Student at FER Zagreb, Croatia
--------------------------------+--------------------------------
Good pings come in small packets.
From: Barry Margolin
Subject: Re: use of mapcar
Date: 
Message-ID: <5mpj4f$fa0@pasilla.bbnplanet.com>
In article <···············@jagor.srce.hr>,
Hrvoje Niksic  <·······@srce.hr> wrote:
>····@bunny.gte.com (Dorai Sitaram) writes:
>
>> In article <·············@math.okstate.edu>,
>> Mark McConnell  <·······@math.okstate.edu> wrote:
>> >First, use (mapcar #'myfunc ...) instead of (mapcar 'myfunc ...)
>> 
>> I'm curious why many stylists suggest this.  'myfunc, of
>> course, works just as well as #'myfunc.  If Common Lisp is
>> offering to distinguish between symbol-function and
>> symbol-value, why not use it?
>
>A reason I've heard is that #'myfunc is looked up in the current
>package, whereas 'myfunc only globally.

Replace "package" with "scope" and you'll be correct; I suspect that is
what was meant.

Another reason is consistency.  While you can use 'myfunc in place of
#'myfunc in most places, you can't use '(lambda ...) in place of #'(lambda
...), according to ANSI CL.  Most implementations will allow a lambda
expression in place of a function, but this is because CLtL permitted it;
CLtL2 and ANSI CL removed this, specifying that only a function or a symbol
can be used.  The only reason why symbols are still allowed is so that you
can store function names in data structures in order to have the
appropriate function called if the function is redefined (if you did (setq
*func* #'myfunc), *func* would always contain the definition at the time of
the setq).

There's also a possible efficiency consideration.  When you reference
#'myfunc, the compiler ensures that the linkage to the function object
occurs once, when the file is loaded.  When you use 'myfunc, the function
that eventually receives the symbol (FUNCALL or APPLY) has to call
SYMBOL-FUNCTION.  Since this is in the inner loop of a mapping function, it
could add up; on the other hand, the mapping function could do this itself,
or a good compiler could transform (mapcar 'symbol ...) into (mapcar
#'symbol ...) automatically.

-- 
Barry Margolin, ······@bbnplanet.com
BBN Corporation, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
From: Lyman S. Taylor
Subject: Re: use of mapcar
Date: 
Message-ID: <5mq33c$bkl@pravda.cc.gatech.edu>
In article <··········@pasilla.bbnplanet.com>,
Barry Margolin  <······@bbnplanet.com> wrote:
>In article <···············@jagor.srce.hr>,
>Hrvoje Niksic  <·······@srce.hr> wrote:
>>····@bunny.gte.com (Dorai Sitaram) writes:
>>
>>> In article <·············@math.okstate.edu>,
>>> Mark McConnell  <·······@math.okstate.edu> wrote:
>>> >First, use (mapcar #'myfunc ...) instead of (mapcar 'myfunc ...)
>>> 
>>> I'm curious why many stylists suggest this.  'myfunc, of
....
>>A reason I've heard is that #'myfunc is looked up in the current
>>package, whereas 'myfunc only globally.
>
>Replace "package" with "scope" and you'll be correct; I suspect that is
>what was meant.

 Illustrative example: 

? (defun myfunc ()  (print "Global Function" ))
MYFUNC
? (labels (  (myfunc () (print "local function"))  )
       (funcall #'myfunc )
       (funcall 'myfunc )
       'demo-done  )

"local function" 
"Global Function" 
DEMO-DONE 
? 


>Another reason is consistency.  While you can use 'myfunc in place of
>#'myfunc in most places, you can't use '(lambda ...) in place of #'(lambda
>...), according to ANSI CL.  Most implementations will allow a lambda
>expression in place of a function, but this is because CLtL permitted it;
>CLtL2 and ANSI CL removed this, specifying that only a function or a symbol
>can be used. 

Macintosh Common Lisp does "puke" when passed a list that happens to 
have lambda as the first element. However, I was surprised that most other
implementations still allow this.  Seems like that would make 
FUNCALL/APPLY as equal a code-shaker "killer" as the use of EVAL is. 

? (defun make-func ( value ) 
    `(lambda ( x) ( + ,value  x )))
MAKE-FUNC
? (make-func 3 )
(LAMBDA (X) (+ 3 X))
? (listp (make-func 3 ) )
T
? (funcall (make-func 3 ) 2 )
.... should be an error but in most environments returns 5.....

There is an implicit eval ( and/or call to the compiler ) to turn that
list into a function. Which means that since you might possibly need
any arbitrary lisp function that nothing can be removed from the 
environment. 

How pervasive is the use of this obsolete "formulation"?  




 
-- 
					
Lyman S. Taylor            "It's not *OUR* fault....."
(·····@cc.gatech.edu)           Kei and Yuri , the 'Dirty Pair'... eerrr
					'Lovely Angels'
From: Howard R. Stearns
Subject: Re: use of mapcar
Date: 
Message-ID: <3392D592.15FB7483@elwoodcorp.com>
Barry Margolin wrote:
....
> There's also a possible efficiency consideration.  When you reference
> #'myfunc, the compiler ensures that the linkage to the function object
> occurs once, when the file is loaded.  When you use 'myfunc, the function
> that eventually receives the symbol (FUNCALL or APPLY) has to call
> SYMBOL-FUNCTION.  Since this is in the inner loop of a mapping function, it
> could add up; on the other hand, the mapping function could do this itself,
> or a good compiler could transform (mapcar 'symbol ...) into (mapcar
> #'symbol ...) automatically.

(What follows is angels-on-the-head-of-a-pin-counting.)

It is not clear to me that the compilation of (function <function-name>)
MUST arrange for the function definition to be grabbed at load time.
For example, if <function-name> refers to a lexical closure over
changing variables, then the function definition MUST be obtained at
run-time, no?

Furthermore, suppose we're working in an implemenation in which there is
some subtle difference between (fdefinition '<function-name>) and
(function <function-name>) even for global functions -- i.e. one in
which the former does a reference to a symbol page in the heap, while
the latter is a reference to a page of compiler-maintained, load-time
"literals" (for lack of a better word).  I believe this is what Barry
has in mind, and I agree that it is probably the way most
implementations work.  As far as I can tell, the only difference between
these two implementations is that their may be different costs between
the two different page references, AND that (setf (fdefinition
<function-name>)) <new-closure>) will effect the symbol page reference,
while the "literal" page reference got "cached" at load-time.

I might (naively?) expect that with high DEBUG optimization declarations
and relatively low SPEED, or with <function-name> declared NOTINLINE,
the compiler-maintained, load-time "literal" approach is not acceptable,
and that in this case, the compiler, after determining that
<function-name> refers to a global definition, must generate basically
the same code as for (fdefinition '<function-name>).  (Assuming that
FDEFINITION was inlined, dead code for type checking eliminated, etc.)
From: Barry Margolin
Subject: Re: use of mapcar
Date: 
Message-ID: <5murr5$dq4@tools.bbnplanet.com>
In article <·················@elwoodcorp.com>,
Howard R. Stearns <······@elwoodcorp.com> wrote:
>It is not clear to me that the compilation of (function <function-name>)
>MUST arrange for the function definition to be grabbed at load time.

No, but it MAY do this, and all good implementations do so.

>For example, if <function-name> refers to a lexical closure over
>changing variables, then the function definition MUST be obtained at
>run-time, no?

No.  The only thing that has to be done at run-time is for the function to
indirect references to the closed-over variables via the environment that's
included as part of the closure.  In most implementations, each closure of
a particular function 

>Furthermore, suppose we're working in an implemenation in which there is
>some subtle difference between (fdefinition '<function-name>) and
>(function <function-name>) even for global functions -- i.e. one in
>which the former does a reference to a symbol page in the heap, while
>the latter is a reference to a page of compiler-maintained, load-time
>"literals" (for lack of a better word).  I believe this is what Barry
>has in mind, and I agree that it is probably the way most
>implementations work.  As far as I can tell, the only difference between
>these two implementations is that their may be different costs between
>the two different page references, AND that (setf (fdefinition
><function-name>)) <new-closure>) will effect the symbol page reference,
>while the "literal" page reference got "cached" at load-time.

This would be invalid.  Every time (function <function-name>) is executed
it must evaluate to the current definition.  A cache is OK, and most
implementations have this, but redefining the function must cause the cache
to be reloaded as necessary.

Note that there's a big difference between:

(funcall #'<function-name> ...)
(defun <function-name> ...)
(funcall #'<function-name> ...)

and:

(setq *function* #'<function-name>)
(funcall *function* ...)
(defun <function-name> ...)
(funcall *function* ...)

The indirection through either the symbol or the loader's symbol table
cache only occurs when #'<function-name> is executed.  In the first case
it's executed each time the FUNCALL is executed, but in the second case
it's only executed at the time of the SETQ.  That snapshots the current
function binding.  This is the reason why FUNCALL and APPLY are allowed to
accept symbols; you can then write that as:

(setq *function* '<function-name>)
(funcall *function* ...)
(defun <function-name> ...)
(funcall *function* ...)

and the expected thing will happen.  Note that there are times when you do
really want to capture the actual binding just once; consider the following
simple definition of the common DEFADVICE macro:

(defmacro defadvice (function-name args &body body)
  `(let ((old-value #',function-name))
     (flet ((call-original (&rest args)
              (apply old-value args)))
       (defun ,function-name ,args ,@body))))

-- 
Barry Margolin, ······@bbnplanet.com
BBN Corporation, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
From: Chuck Fry
Subject: Re: use of mapcar
Date: 
Message-ID: <5mv43m$4tu$1@shell5.ba.best.com>
In article <··········@news.gte.com>, Dorai Sitaram <····@bunny.gte.com> wrote:
>In article <·············@math.okstate.edu>,
>Mark McConnell  <·······@math.okstate.edu> wrote:
>>First, use (mapcar #'myfunc ...) instead of (mapcar 'myfunc ...)
>
>I'm curious why many stylists suggest this.  'myfunc, of
>course, works just as well as #'myfunc.  If Common Lisp is
>offering to distinguish between symbol-function and
>symbol-value, why not use it?

This is kind of a hackish optimization.  

In compiled code, 
 (mapcar #'myfunc ...) 
 says to use the *current definition at compile time* of the function
myfunc, and to pass the function definition rather than the symbol
myfunc.

Conversely,
 (mapcar 'myfunc ...)
 says to use the symbol myfunc, and to look at its function binding at
*run time*.

The "optimization" is that using #'myfunc instead of 'myfunc saves one
memory reference (indirecting through the symbol's function slot) per
call.  The cost is a reference to a possibly obsolete version of myfunc.

Of course, in the Lispm world, you'd always get the current
definition... but most of us aren't using Lispms any more.
 -- Chuck
-- 
	    Chuck Fry -- Jack of all trades, master of none
 ······@chucko.com (text only please), ········@home.com (MIME enabled)
		      This space for rent... NOT.
From: Donald H. Mitchell
Subject: Re: use of mapcar
Date: 
Message-ID: <338C5D1F.145BF0E1@smartproject.com>
Stuart Hirons wrote:

> Hello, trying to use ' mapcar' on a list and call a function with
> each of the items in the list, plus 2 other params which remain
> constant throughout.
>
> i.e.
>
> (defun myfunc (a b c)
>    (cond
>       .....various guards etc go here...
>      (t (mapcar 'myfunc '(cat dog lion) b c))))

Depending on mood (or whether I need a lambda for other reasons), I
either do as barmar and McConnell suggested or create a circular list
out of the other args. map fns stop as soon as they come to the end of
the shortest list; thus, given a fn circular-list (defined below), you
could write your example as

...(mapcar #'myfunc '(cat dog lion) (circular-list b) (circular-list
c))...

(defun circular-list (&rest elements)
  "Make a circular list out of the elements where the last cons points
back to the beginning of the list."
  (setf elements (copy-list elements))
  (nconc elements elements))
--
Donald H. Mitchell, PhD, PMP    ··········@smartproject.com
Proactive Solutions, Inc.       http://home.earthlink.net/~smartproject
412.835.2410                    412.835.2411 (fax)