From: Jonathon McKitrick
Subject: mapcar, mapcar, mapcar... what about the others?
Date: 
Message-ID: <1141882120.724914.176470@u72g2000cwu.googlegroups.com>
I keep saying to myself, 'A-ha!  Here's a chance to try a different
variation of mapping.'  Then I go to the Hyperspec again, and find
that... once again... it boils down to mapcar.

The problems I am solving are not that complex at the mapping level.
But I'd be interested in seeing some examples that are more than just
theory, and show uses for the others.

One variation I tried was converting a record from clsql, which comes
with a list of field names, into an xml structure.  Mapcar does this,
but since I don't really care about the result when the function I use
is producing the output, mapc works as well.  Am I misusing mapc in
this instance?

(mapc #'xml-emitter:simple-tag fields values)

This works great to map a record to xml and send it as an ajax
response, BTW.

From: Alexander Schmolck
Subject: Re: mapcar, mapcar, mapcar... what about the others?
Date: 
Message-ID: <yfsoe0g9blh.fsf@oc.ex.ac.uk>
"Jonathon McKitrick" <···········@bigfoot.com> writes:

> I keep saying to myself, 'A-ha!  Here's a chance to try a different
> variation of mapping.'  Then I go to the Hyperspec again, and find
> that... once again... it boils down to mapcar.
> 
> The problems I am solving are not that complex at the mapping level.
> But I'd be interested in seeing some examples that are more than just
> theory, and show uses for the others.

map ought to be obvious. After that mapcan is I think the next most useful --
you can e.g. use it for things for which you'd use list comprehensions in
python or haskell.

1. As a "map-if"

    python: [f(x) for x in xs if predicate(x)]
    cl: (mapcan (lambda (x) (when (predicate x) (list (f x)))) xs)
        (loop for x in xs when (predicate x) collect x)

    Example: Return the sqrt of all non-negative numbers in a list.

2. As a mapcar that flattens 1 level:

    python: [f(y) for x in xs for y in g(x)]
    cl: (mapcan (lambda (x) (mapcar #'f (g x))) xs)
        (loop for x in xs nconc (loop for y in (g x) collect (f y)))

    Example: Cartesian product of two lists.

'as
From: David Sletten
Subject: Re: mapcar, mapcar, mapcar... what about the others?
Date: 
Message-ID: <PATPf.7983$e1.4365@tornado.socal.rr.com>
Jonathon McKitrick wrote:

> I keep saying to myself, 'A-ha!  Here's a chance to try a different
> variation of mapping.'  Then I go to the Hyperspec again, and find
> that... once again... it boils down to mapcar.
> 
> The problems I am solving are not that complex at the mapping level.
> But I'd be interested in seeing some examples that are more than just
> theory, and show uses for the others.
> 

I searched through my code and the only non-contrived use of the rarer 
maps I found was this:
(defun sublists (l)
   (maplist #'identity l))

I suppose nowadays I probably lean towards using LOOP for all but 
straightforward MAPCAR situations:
(defun sublists (l)
   (loop for cons on l by #'cdr collect cons))

> One variation I tried was converting a record from clsql, which comes
> with a list of field names, into an xml structure.  Mapcar does this,
> but since I don't really care about the result when the function I use
> is producing the output, mapc works as well.  Am I misusing mapc in
> this instance?
> 
> (mapc #'xml-emitter:simple-tag fields values)
> 

I think this is exactly what MAPC is for. An alternative would be:
(map nil #'xml-emitter:simple-tag fields values)

Of course for single lists, DOLIST is simpler. And while LOOP may be 
more verbose here, it's probably clearer too:
(loop for field in fields
       for value in values
       do (xml-emitter:simple-tag field value))

Aloha,
David Sletten
From: Pascal Costanza
Subject: Re: mapcar, mapcar, mapcar... what about the others?
Date: 
Message-ID: <47afbmFeohu9U1@individual.net>
David Sletten wrote:
> Jonathon McKitrick wrote:
> 
>> I keep saying to myself, 'A-ha!  Here's a chance to try a different
>> variation of mapping.'  Then I go to the Hyperspec again, and find
>> that... once again... it boils down to mapcar.
>>
>> The problems I am solving are not that complex at the mapping level.
>> But I'd be interested in seeing some examples that are more than just
>> theory, and show uses for the others.
>>
> 
> I searched through my code and the only non-contrived use of the rarer 
> maps I found was this:
> (defun sublists (l)
>   (maplist #'identity l))
> 
> I suppose nowadays I probably lean towards using LOOP for all but 
> straightforward MAPCAR situations:
> (defun sublists (l)
>   (loop for cons on l by #'cdr collect cons))

(loop for cons on l collect cons) is sufficient here.


Pascal

-- 
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/
From: David Sletten
Subject: Re: mapcar, mapcar, mapcar... what about the others?
Date: 
Message-ID: <X_TPf.20723$992.84@tornado.socal.rr.com>
Pascal Costanza wrote:


>> (defun sublists (l)
>>   (loop for cons on l by #'cdr collect cons))
> 
> 
> (loop for cons on l collect cons) is sufficient here.
> 

Um, yeah. Right. Oops.

Thanks, Pascal.

David Sletten
From: Andreas Thiele
Subject: Re: mapcar, mapcar, mapcar... what about the others?
Date: 
Message-ID: <dup21c$394$02$1@news.t-online.com>
"Jonathon McKitrick" <···········@bigfoot.com> schrieb im Newsbeitrag 
·····························@u72g2000cwu.googlegroups.com...
>...

mapcar applies it's function to a list and returns this new list. mapc just 
iterates and doesn't create a list. I'd consider mapc applied to a single 
list of no good readability. It is pure imperative style (looking somehow 
pretentious functional) and thus loop, do or dolist would provide better 
readability.

Andreas