From: Kenny Tilton
Subject: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <bdXcc.4952$mX.2849759@twister.nyc.rr.com>
I see the MAP family always stops at the shortest of multiple lists 
being iterated over, and my naive attempts at persuading LOOP to soldier 
on through the longest while binding nil to the other iteration vars 
have been rejected all.

Am I missing a trick on LOOP, or is a job only DO can do? If it helps, 
in my particular case I have only two possibilities: (a) two lists of 
equal length, or (b) one list of plusp length, one nil.

kt

-- 
Home? http://tilton-technology.com
Cells? http://www.common-lisp.net/project/cells/
Cello? http://www.common-lisp.net/project/cello/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application

From: Chris Perkins
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <6cb6c81f.0404071600.522ab305@posting.google.com>
Kenny Tilton <·······@nyc.rr.com> wrote in message news:<·····················@twister.nyc.rr.com>...
> I see the MAP family always stops at the shortest of multiple lists 
> being iterated over, and my naive attempts at persuading LOOP to soldier 
> on through the longest while binding nil to the other iteration vars 
> have been rejected all.
> 
> Am I missing a trick on LOOP, or is a job only DO can do? If it helps, 
> in my particular case I have only two possibilities: (a) two lists of 
> equal length, or (b) one list of plusp length, one nil.
> 
> kt

I had a similar need awhile ago, and wrote the following.  It is
slightly schem-ish (set! push!), and has never been optimized:

  ;map only goes as longs as the first list,
  ; whereas for-each doesn't return a value 
  ; this one goes until all lists are exhausted.
(defun map-completely (f &rest lists)
  (let ((args (map car lists))
	  (result (list)))
    (until (every (lambda (a) (null? a)) args)
	(push! (apply f args) result)
	(set! lists (map cdr lists))
	(set! args (map car lists)))
    (reverse result)))


Chris
From: Pascal Costanza
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <c51gh0$hgp$1@newsreader2.netcologne.de>
Kenny Tilton wrote:

> I see the MAP family always stops at the shortest of multiple lists 
> being iterated over, and my naive attempts at persuading LOOP to soldier 
> on through the longest while binding nil to the other iteration vars 
> have been rejected all.
> 
> Am I missing a trick on LOOP, or is a job only DO can do? If it helps, 
> in my particular case I have only two possibilities: (a) two lists of 
> equal length, or (b) one list of plusp length, one nil.

What about...

(loop for a in '(1 2 3 4 5)
       for b* = '(1 2 3) then (cdr b*)
       for b = (car b*)
       do (print a)
       do (print b))


Pascal

-- 
1st European Lisp and Scheme Workshop
June 13 - Oslo, Norway - co-located with ECOOP 2004
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/
From: Kenny Tilton
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <OfYcc.4956$mX.2865788@twister.nyc.rr.com>
Pascal Costanza wrote:

> 
> Kenny Tilton wrote:
> 
>> I see the MAP family always stops at the shortest of multiple lists 
>> being iterated over, and my naive attempts at persuading LOOP to 
>> soldier on through the longest while binding nil to the other 
>> iteration vars have been rejected all.
>>
>> Am I missing a trick on LOOP, or is a job only DO can do? If it helps, 
>> in my particular case I have only two possibilities: (a) two lists of 
>> equal length, or (b) one list of plusp length, one nil.
> 
> 
> What about...
> 
> (loop for a in '(1 2 3 4 5)
>       for b* = '(1 2 3) then (cdr b*)

ah, there it is. smashing. thx, kt

>       for b = (car b*)
>       do (print a)
>       do (print b))
> 
> 
> Pascal
> 

-- 
Home? http://tilton-technology.com
Cells? http://www.common-lisp.net/project/cells/
Cello? http://www.common-lisp.net/project/cello/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application
From: Peter Seibel
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <m3smffsgpp.fsf@javamonkey.com>
Pascal Costanza <········@web.de> writes:

> Kenny Tilton wrote:
>
>> I see the MAP family always stops at the shortest of multiple lists
>> being iterated over, and my naive attempts at persuading LOOP to
>> soldier on through the longest while binding nil to the other
>> iteration vars have been rejected all.
>> Am I missing a trick on LOOP, or is a job only DO can do? If it
>> helps, in my particular case I have only two possibilities: (a) two
>> lists of equal length, or (b) one list of plusp length, one nil.
>
> What about...
>
> (loop for a in '(1 2 3 4 5)
>        for b* = '(1 2 3) then (cdr b*)
>        for b = (car b*)
>        do (print a)
>        do (print b))
>

Or a bit more tricksy:

  (loop for a in '(1 2 3 4 5)
        for (b . rest) = '(1 2 3) then rest
        do (print a) (print b))

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Kenny Tilton
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <pqZcc.4962$mX.2878771@twister.nyc.rr.com>
Peter Seibel wrote:
> Pascal Costanza <········@web.de> writes:
> 
> 
>>Kenny Tilton wrote:
>>
>>
>>>I see the MAP family always stops at the shortest of multiple lists
>>>being iterated over, and my naive attempts at persuading LOOP to
>>>soldier on through the longest while binding nil to the other
>>>iteration vars have been rejected all.
>>>Am I missing a trick on LOOP, or is a job only DO can do? If it
>>>helps, in my particular case I have only two possibilities: (a) two
>>>lists of equal length, or (b) one list of plusp length, one nil.
>>
>>What about...
>>
>>(loop for a in '(1 2 3 4 5)
>>       for b* = '(1 2 3) then (cdr b*)
>>       for b = (car b*)
>>       do (print a)
>>       do (print b))
>>
> 
> 
> Or a bit more tricksy:
> 
>   (loop for a in '(1 2 3 4 5)
>         for (b . rest) = '(1 2 3) then rest
>         do (print a) (print b))

Cool. And this (what I actually needed) even works:

(loop for a in '(1 2 3 4 5)
     for ((b c) . rest) = '((1 2)(2 3)(3 4)) then rest
     do (print (list a b c)))

I had also been meaning to whine here about there being no place to 
declare, say in this example, ignore on b, but my loop surfing unearthed:

(loop for a in '(1 2 3 4 5)
     for ((nil c) . rest) = '((1 2)(2 3)(3 4)) then rest
     do (print (list a c)))

Lisp is such a sensible language, but Loop is nice comic relief. Is loop 
  the crazy aunt living in the basement of the crazy aunt living in the 
basement? Does loop have some odd feature, odd even for loop? Is it 
aunts in the basement all the way down?

If, however, loop has some normal feature in the midst of its oddity, 
now we're talking about peloria, a splendid word: the abnormal 
appearance of regularity in a normally irregular pattern.

Now I must skate. Screen shot tonight of the Cello scroller widget, and 
you don't want to miss it. This will be more controversial than Janet 
Jackson's Super Bowl "costume malfunction" and/or Bob Dylan's Victoria's 
Secret ad. Christophe can start preparing his counter-shot now.

MWUAHAHHHAHHA-MWUA-MWWUAHHA-MWWUUAAHAHAHAAA

kt

-- 
Home? http://tilton-technology.com
Cells? http://www.common-lisp.net/project/cells/
Cello? http://www.common-lisp.net/project/cello/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application
From: Marc Spitzer
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <86oeq31nxk.fsf@bogomips.optonline.net>
Kenny Tilton <·······@nyc.rr.com> writes:

> Now I must skate. Screen shot tonight of the Cello scroller widget,
> and you don't want to miss it. This will be more controversial than
> Janet Jackson's Super Bowl "costume malfunction" and/or Bob Dylan's
> Victoria's Secret ad. Christophe can start preparing his counter-shot
> now.

visions of Bob Dylan in a teddy <SHUDDER> URK ICK.

Do not post this before dinner, its just wrong.

marc
From: Anton van Straaten
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <Zo_cc.329$l75.207@newsread2.news.atl.earthlink.net>
Marc Spitzer wrote:

> Kenny Tilton <·······@nyc.rr.com> writes:
>
> > Now I must skate. Screen shot tonight of the Cello scroller widget,
> > and you don't want to miss it. This will be more controversial than
> > Janet Jackson's Super Bowl "costume malfunction" and/or Bob Dylan's
> > Victoria's Secret ad. Christophe can start preparing his counter-shot
> > now.
>
> visions of Bob Dylan in a teddy <SHUDDER> URK ICK.

That, presumably, would be on the night of his gay wedding to...  Mick
Jagger?

> Do not post this before dinner, its just wrong.

Hey, you're the one who upped the ante...

Anton
From: Gareth McCaughan
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <87y8p7v8s2.fsf@g.mccaughan.ntlworld.com>
Pascal Costanza <········@web.de> writes:

> Kenny Tilton wrote:
> 
> > I see the MAP family always stops at the shortest of multiple lists
> > being iterated over, and my naive attempts at persuading LOOP to
> > soldier on through the longest while binding nil to the other
> > iteration vars have been rejected all.
> > Am I missing a trick on LOOP, or is a job only DO can do? If it
> > helps, in my particular case I have only two possibilities: (a) two
> > lists of equal length, or (b) one list of plusp length, one nil.
> 
> What about...
> 
> (loop for a in '(1 2 3 4 5)
>        for b* = '(1 2 3) then (cdr b*)
>        for b = (car b*)
>        do (print a)
>        do (print b))

That assumes that you know which list is the potentially longer
one.

    (loop for a* = a-list then (cdr a*)
          for b* = b-list then (cdr b*)
          for a = (car a*)
          for b = (car b*)
          while (or a* b*)
          do (format t "~&~A ~A~%" a b))

Though if your application is reasonably simple, I'd be more
inclined to do

    (defmacro dolists (clauses &body body)
      (let ((temps (loop for clause in clauses collect (gensym))))
        `(do ,(loop for (var list-form) in clauses for temp in temps
                    collect `(,temp ,list-form (cdr ,temp)))
             ((not (or . ,temps)))
             (let ,(loop for (var list-form) in clauses for temp in temps
                         collect `(,var (first ,temp)))
               . ,body))))

    (dolists ((a '(1 2 3 4 5))
              (b '(a b c)))
      (format t "~&~A ~A~%" a b))

-- 
Gareth McCaughan
.sig under construc
From: Wolfhard Buß
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <m3smff7e0q.fsf@buss-14250.user.cis.dfn.de>
* Kenny Tilton writes:

> I see the MAP family always stops at the shortest of multiple lists
> being iterated over, and my naive attempts at persuading LOOP to
> soldier on through the longest while binding nil to the other
> iteration vars have been rejected all.


 (defun mapkar (function list &rest lists)
   (loop with lists = (copy-list (cons list lists))
         with args = (copy-list lists)
         while (loop for columns on lists
                     for args-rest on args
                     for argsp = (first columns) then (or argsp (first columns))
                     do (setf (first args-rest) (pop (first columns)))
                     finally (return argsp))
         collect (apply function args)))

Example:

 (mapkar #'list '(1 2 3)'(1 2 3 4) '(1))

 => ((1 1 1) (2 2 NIL) (3 3 NIL) (NIL 4 NIL))


-- 
"Hurry if you still want to see something. Everything is vanishing."
                                       --  Paul C�zanne (1839-1906)
From: Wolfhard Buß
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <m365ca237b.fsf@buss-14250.user.cis.dfn.de>
* Kenny Tilton:

> I see the MAP family always stops at the shortest of multiple lists
> being iterated over, and my naive attempts at persuading LOOP to
> soldier on through the longest while binding nil to the other
> iteration vars have been rejected all.

* Wolfhard Bu�:

>  (mapkar #'list '(1 2 3) '(1 2 3 4) '(1))
>
>  => ((1 1 1) (2 2 NIL) (3 3 NIL) (NIL 4 NIL))

Additional note to the newby:

Lispniks know that symbol mapkar is an unsuitable function name.  Its
sound is already assigned to mapcar.  mapcar* seems possible.


-- 
"Hurry if you still want to see something. Everything is vanishing."
                                       --  Paul C�zanne (1839-1906)
From: Pascal Costanza
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <c53976$nrn$1@newsreader2.netcologne.de>
> Lispniks know that symbol mapkar is an unsuitable function name.  Its
> sound is already assigned to mapcar.  mapcar* seems possible.

How many soundspaces does Common Lisp have, then? ;)


Pascal

-- 
1st European Lisp and Scheme Workshop
June 13 - Oslo, Norway - co-located with ECOOP 2004
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/
From: Wolfhard Buß
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <m3y8p6zjcy.fsf@buss-14250.user.cis.dfn.de>
>> Lispniks know that symbol mapkar is an unsuitable function name.  Its
>> sound is already assigned to mapcar.  mapcar* seems possible.
>
> How many soundspaces does Common Lisp have, then? ;)

Lisp's soundspaces correspond to her namespaces. That's not the point.
Symbol's sounds are for the talking Lispnik appreciating that equal
sounds unambigously refer to a certain symbol in the context of a
certain package.

-- 
"Hurry if you still want to see something. Everything is vanishing."
                                       --  Paul C�zanne (1839-1906)
From: Sunnan
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <87r7uyr1ut.fsf@handgranat.org>
·····@gmx.net (Wolfhard Bu�) writes:
> Lisp's soundspaces correspond to her namespaces.

I'd say that she has only one soundspace in a sense, but since
homophonity isn't semantically significant, you could also say that
she has an infinite number of soundspaces.

-- 
One love,
Sunnan
From: Christophe Rhodes
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <sqfzbe1zeu.fsf@lambda.dyndns.org>
·····@gmx.net (Wolfhard Buß) writes:

> Lispniks know that symbol mapkar is an unsuitable function name.  Its
> sound is already assigned to mapcar.  mapcar* seems possible.

Interesting.  To me, mapcar and mapcar* sound the same... maybe I need
to take a leaf from Victor Borge?

Christophe
-- 
http://www-jcsu.jesus.cam.ac.uk/~csr21/       +44 1223 510 299/+44 7729 383 757
(set-pprint-dispatch 'number (lambda (s o) (declare (special b)) (format s b)))
(defvar b "~&Just another Lisp hacker~%")    (pprint #36rJesusCollegeCambridge)
From: Wolfhard Buß
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <m3u0zuzh4o.fsf@buss-14250.user.cis.dfn.de>
* Wolfhard Bu�:

> Lispniks know that symbol mapkar is an unsuitable function name.  Its
> sound is already assigned to mapcar.  mapcar* seems possible.

* Christophe Rhodes:

> Interesting.  To me, mapcar and mapcar* sound the same... maybe I need
> to take a leaf from Victor Borge?


A Lispnik using the dostar sound to denote do* not do seems
possible. No?

-- 
"Hurry if you still want to see something. Everything is vanishing."
                                       --  Paul C�zanne (1839-1906)
From: Rob Warnock
Subject: Re: newby Q: iterating over /longest/ of multiple lists
Date: 
Message-ID: <UfKdnc2coY83qujd4p2dnA@speakeasy.net>
Kenny Tilton  <·······@nyc.rr.com> wrote:
+---------------
| I see the MAP family always stops at the shortest of multiple lists being
| iterated over, and my naive attempts at persuading LOOP to soldier on...
...
| Am I missing a trick on LOOP, or is a job only DO can do? If it helps, 
| in my particular case I have only two possibilities: (a) two lists of 
| equal length, or (b) one list of plusp length, one nil.
+---------------

Others have given you good options, but I simply couldn't let
this go by without sharing one of my favorite abuses of LOOP:  ;-}

    > (loop for i in '(0 1 2 3 4 5 6 7 8 9)
	    and j in '#1=(a b c . #1#)
	collect (list i j))

    ((0 A) (1 B) (2 C) (3 A) (4 B) (5 C) (6 A) (7 B) (8 C) (9 A))
    > 


-Rob

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