From: Jonathon McKitrick
Subject: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154560660.425353.243960@h48g2000cwc.googlegroups.com>
It seems to me that popular opinion leans toward LOOP in favor of map
and variants where either would apply, for the following reasons, to
mention a few:

1.  LOOP is more readable
2.  LOOP is more efficient
3.  LOOP forms are more easily modified and expanded

Are these the main reasons, or are there others?  Any to the contrary?

From: ajones
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154561015.530277.244070@s13g2000cwa.googlegroups.com>
Jonathon McKitrick wrote:
> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:
>
> 1.  LOOP is more readable
> 2.  LOOP is more efficient
> 3.  LOOP forms are more easily modified and expanded
>
> Are these the main reasons, or are there others?  Any to the contrary?

You know, I have done next to no Lisp programming (still working my way
through PCL) and I have to say that, at the moment, I hate loop. I
should love it, it handles pretty much every looping mechanism under
the sun, but is not brain dead like the rest of them are. In thinking
about it though, I have the same problem with the *other* Lisp
mini-language (format), so maybe it is just my brain rejecting the
concept of trying to learn three languages at once.
From: Ken Tilton
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <eibAg.61$rH3.31@fe11.lga>
ajones wrote:
> Jonathon McKitrick wrote:
> 
>>It seems to me that popular opinion leans toward LOOP in favor of map
>>and variants where either would apply, for the following reasons, to
>>mention a few:
>>
>>1.  LOOP is more readable
>>2.  LOOP is more efficient
>>3.  LOOP forms are more easily modified and expanded
>>
>>Are these the main reasons, or are there others?  Any to the contrary?
> 
> 
> You know, I have done next to no Lisp programming (still working my way
> through PCL) and I have to say that, at the moment, I hate loop. I
> should love it, it handles pretty much every looping mechanism under
> the sun, but is not brain dead like the rest of them are. In thinking
> about it though, I have the same problem with the *other* Lisp
> mini-language (format), so maybe it is just my brain rejecting the
> concept of trying to learn three languages at once.

Yeah, took me forever (and PCL) before I broke down and sorted out the 
unpredictable syntax, but now I do not use anything

Yeah, took me forever and a PCL before I broke down and learned loop. 
Big mistake. Iteration is so common in code, it is worth the trouble. I 
am still not that great at format, but I do not use it much and now at 
least I know what is there in case I need to write out the lyrics to 99 
Beers On the Wall.

My2: Learn loop now. Just a coupla hours with PCL should do it.

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Thomas A. Russ
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <ymi4pwty8br.fsf@sevak.isi.edu>
Jonathon McKitrick wrote:
> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:
>
> 1.  LOOP is more readable
> 2.  LOOP is more efficient
> 3.  LOOP forms are more easily modified and expanded
>
> Are these the main reasons, or are there others?  Any to the contrary?

Contrary:

1.  For the standard cases, MAPx is more concise.
2.  The mapping functions can work with any kind of sequence without the
    need to switch the syntax.  Compare

    (mapcar #'+ '(1 2 3) #(3 2 1))

    (loop for i in '(1 2 3)
          as j across #(3 2 1)
          collect (+ i j))

3.  The mapping functions make it easier to add additional arguments to
    varible arity functions.
4.  The general MAP function can return a sequence of any kind.


And for a mostly theoretical issue:

T1. MAPx can be implemented in parallel with non-side-effecting
    functions.  This is generally not done, however.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Pascal Bourguignon
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <87psfhmy4g.fsf@thalassa.informatimago.com>
···@sevak.isi.edu (Thomas A. Russ) writes:

> Jonathon McKitrick wrote:
>> It seems to me that popular opinion leans toward LOOP in favor of map
>> and variants where either would apply, for the following reasons, to
>> mention a few:
>>
>> 1.  LOOP is more readable
>> 2.  LOOP is more efficient
>> 3.  LOOP forms are more easily modified and expanded
>>
>> Are these the main reasons, or are there others?  Any to the contrary?
>
> Contrary:
>
> 1.  For the standard cases, MAPx is more concise.
> 2.  The mapping functions can work with any kind of sequence without the
>     need to switch the syntax.  Compare
>
>     (mapcar #'+ '(1 2 3) #(3 2 1))

You mean MAP (or MAP-INTO):

      (MAP 'list (function +) '(1 2 3) #(3 2 1))


>     (loop for i in '(1 2 3)
>           as j across #(3 2 1)
>           collect (+ i j))
>
> 3.  The mapping functions make it easier to add additional arguments to
>     varible arity functions.
> 4.  The general MAP function can return a sequence of any kind.
>
>
> And for a mostly theoretical issue:
>
> T1. MAPx can be implemented in parallel with non-side-effecting
>     functions.  This is generally not done, however.
>
> -- 
> Thomas A. Russ,  USC/Information Sciences Institute

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush
From: ········@gmail.com
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154563594.734658.18080@75g2000cwc.googlegroups.com>
Jonathon McKitrick wrote:
> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:
>
> 1.  LOOP is more readable
> 2.  LOOP is more efficient
> 3.  LOOP forms are more easily modified and expanded
>
> Are these the main reasons, or are there others?  Any to the contrary?

I would argue that LOOP forms are less visually intuitive.

jkf's "Lisp Coding Standards v1.0" [1] says it well:

"Use do, dotimes and dolist for iteration in place of the extended loop
macro (the one using the keywords).   The structure of the code inside
a loop is not apparent.   Thus a function with loop in it is opaque
until you've read carefully through all the keywords used in the
expression."

personally I prefer map and variants to the DO or LOOP families...

Nick

(1) http://www.franz.com/~jkf/coding_standards.html
From: Matthew D Swank
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <pan.2006.08.03.20.44.07.570387@c.net>
On Wed, 02 Aug 2006 17:06:34 -0700, nallen05 wrote:

> jkf's "Lisp Coding Standards v1.0" [1] says it well:
> 
> "Use do, dotimes and dolist for iteration in place of the extended loop
> macro (the one using the keywords).   The structure of the code inside
> a loop is not apparent.   Thus a function with loop in it is opaque
> until you've read carefully through all the keywords used in the
> expression."

I really loathed 'do' until I realized it looked like a "'let' in motion".
Though, it looks so much like functional code, I would sometimes forget
that each iteration does not generate a fresh set of bindings for the loop
variables.  This, of course, can cause problems when you close over one of
them.

Matt

-- 
"You do not really understand something unless you can
 explain it to your grandmother." — Albert Einstein.
From: Jonathon McKitrick
Subject: The SPLICE must FLOW....
Date: 
Message-ID: <1154658793.268255.299140@h48g2000cwc.googlegroups.com>
Matthew D Swank wrote:
> I really loathed 'do' until I realized it looked like a "'let' in motion".

'It is through will alone I LET my mind in motion....'
From: Christopher Browne
Subject: Re: The SPLICE must FLOW....
Date: 
Message-ID: <87vep6wq6f.fsf@wolfe.cbbrowne.com>
"Jonathon McKitrick" <···········@bigfoot.com> wrote:
> Matthew D Swank wrote:
>> I really loathed 'do' until I realized it looked like a "'let' in motion".
>
> 'It is through will alone I LET my mind in motion....'

But which juice is it by which thoughts acquire speed?  Perhaps that
from the beans of coffee???
-- 
"cbbrowne",·@","gmail.com"
http://linuxdatabases.info/info/lisp.html
"Whenever you  find that you  are on the  side of the majority,  it is
time to reform." -- Mark Twain
From: Rob Warnock
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <N9CdnZSJ0pv7VU_ZnZ2dnUVZ_v2dnZ2d@speakeasy.net>
Matthew D Swank  <·······································@c.net> wrote:
+---------------
| I really loathed 'do' until I realized it looked like a "'let' in motion".
| Though, it looks so much like functional code, I would sometimes forget
| that each iteration does not generate a fresh set of bindings for the
| loop variables.
+---------------

This is one of the "gotchas" I occasionally bumped into while I was
in the process of moving over from Scheme to CL several years ago.
The Scheme spec requires that each iteration generate a fresh set
of bindings for the variables, while the CLHS requires that the
variables be bound but once and then subsequently assigned-to
["in parallel, as if by PSETQ" for DO, and "sequentially, as if
by SETQ" for DO*].


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Pascal Costanza
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <4jf9f4F7mhopU1@individual.net>
Matthew D Swank wrote:
> On Wed, 02 Aug 2006 17:06:34 -0700, nallen05 wrote:
> 
>> jkf's "Lisp Coding Standards v1.0" [1] says it well:
>>
>> "Use do, dotimes and dolist for iteration in place of the extended loop
>> macro (the one using the keywords).   The structure of the code inside
>> a loop is not apparent.   Thus a function with loop in it is opaque
>> until you've read carefully through all the keywords used in the
>> expression."
> 
> I really loathed 'do' until I realized it looked like a "'let' in motion".
> Though, it looks so much like functional code, I would sometimes forget
> that each iteration does not generate a fresh set of bindings for the loop
> variables.  This, of course, can cause problems when you close over one of
> them.

...but is easy to resolve:

(let (result)
   (do ((some-var ...))
       (...)
     (let ((some-var some-var)) ; create new binding for current value
       (push (lambda (...) ... some-var ...) result)))
   (nreverse result))


Pascal

-- 
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/
From: Ken Tilton
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1cbAg.60$rH3.21@fe11.lga>
Jonathon McKitrick wrote:
> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:
> 
> 1.  LOOP is more readable
> 2.  LOOP is more efficient
> 3.  LOOP forms are more easily modified and expanded
> 
> Are these the main reasons, or are there others?

- Loop is more concise. Related: I do not have to type l-a-m-b-d-a. I 
hate typing l-a-m-b-d-a.

- You missed a rather huge difference: ...no, this is not a bullet. We 
return to prose mode...

You cannot compare map* and loop. Loop is an iteration language, with 
many bells and whistles. Map*s just map.

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Pascal Bourguignon
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <87mzamslo1.fsf@thalassa.informatimago.com>
"Jonathon McKitrick" <···········@bigfoot.com> writes:

> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:
>
> 1.  LOOP is more readable

Depends of your frame.


> 2.  LOOP is more efficient

Depends on the compiler.


> 3.  LOOP forms are more easily modified and expanded

Not always.


> Are these the main reasons, or are there others?  Any to the contrary?

One advantage of map* is that you can easily build them step by step
at the REPL:

(mapcar (lambda (x) (some-processing x)) data)

then when the output is what you expect,

(mapcar (lambda (x) (some-more-processing x))
        (mapcar (lambda (x) (some-processing x)) data))

then some more:

(mapcar (lambda (x) (some-very-more-processing x))
        (mapcar (lambda (x) (some-more-processing x))
                (mapcar (lambda (x) (some-processing x)) data)))

etc.  This "filter" frame can be more natural than a loop some times.
(But frankly, in general I eventually rewrite them as LOOPs, once
bottom-up'ed).


Also, when you have a parallel processor (MMX, SSE, Altivec, etc), 
it's easier for the compilers to optimize MAP* than LOOP.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Do not adjust your mind, there is a fault in reality"
 -- on a wall many years ago in Oxford.
From: Joe Marshall
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154627082.897943.67770@75g2000cwc.googlegroups.com>
Jonathon McKitrick wrote:
> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply

Allow me to be the token knee-jerk anti-loopist.  I hate LOOP.

>, for the following reasons, to
> mention a few:
>
> 1.  LOOP is more readable

I don't know what you mean by readable.  It certainly sounds more like
english when you read it out loud from left to right, but I find it to
be less easy to predict what the resulting code will look like after it
is expanded.

> 2.  LOOP is more efficient

Than MAP?  Loop should be as efficient as the constructs it expands
into.  There is no reason that MAP cannot be as efficient.

> 3.  LOOP forms are more easily modified and expanded

I don't know how you define `ease of modification'.  I've never found
mapping constructs that hard to modify.

> Are these the main reasons, or are there others?  Any to the contrary?

My primary objection to LOOP is that it is extremely difficult to write
meta-level code that can manipulate loop expressions.  A MAP expression
is just a function call, but LOOP requires a serious state-machine to
parse.  You can macro-expand the LOOP away, but the resulting expansion
is almost as hard to understand as the original.

My second objection to LOOP is that it operates at a low level of
abstraction.  One very frequent use of iteration is when you wish to
apply an operation to a collection of objects.  You shouldn't have to
specify how the collection is traversed, and you certainly shouldn't
have to specify a serial traversal algorithm if it doesn't matter.
LOOP intertwines the operation you wish to perform with the minutiae of
traversal --- finding an initial element, stepping to the next element,
stopping when done.  I don't care about these details.  I don't care if
the program LOOPs, uses recursion, farms out the work to other
processors, or whatever, so long as it produces the answer I want.
From: Ari Krupnik
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <868xm5fre3.fsf@deb.lib.aero>
"Joe Marshall" <··········@gmail.com> writes:

> My second objection to LOOP is that it operates at a low level of
> abstraction.  One very frequent use of iteration is when you wish to
> apply an operation to a collection of objects.  You shouldn't have to
> specify how the collection is traversed, and you certainly shouldn't
> have to specify a serial traversal algorithm if it doesn't matter.

That last would seem to be an argument against mapcar, no?

Ari.


-- 
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
From: Joe Marshall
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154716288.164105.169310@i42g2000cwa.googlegroups.com>
Ari Krupnik wrote:
> "Joe Marshall" <··········@gmail.com> writes:
>
> > My second objection to LOOP is that it operates at a low level of
> > abstraction.  One very frequent use of iteration is when you wish to
> > apply an operation to a collection of objects.  You shouldn't have to
> > specify how the collection is traversed, and you certainly shouldn't
> > have to specify a serial traversal algorithm if it doesn't matter.
>
> That last would seem to be an argument against mapcar, no?

It could be, if I relied on mapcar using left-to-right traversal.  I
try to avoid depending on that.
From: franks
Subject: ITERATE vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154640539.157100.140610@i42g2000cwa.googlegroups.com>
Joe Marshall wrote:
> Jonathon McKitrick wrote:
> > It seems to me that popular opinion leans toward LOOP in favor of map
> > and variants where either would apply
>
> Allow me to be the token knee-jerk anti-loopist.  I hate LOOP.
>
> >, for the following reasons, to
> > mention a few:
> >
> > 1.  LOOP is more readable
>
> I don't know what you mean by readable.  It certainly sounds more like
> english when you read it out loud from left to right, but I find it to
> be less easy to predict what the resulting code will look like after it
> is expanded.
>
> > 2.  LOOP is more efficient
>
> Than MAP?  Loop should be as efficient as the constructs it expands
> into.  There is no reason that MAP cannot be as efficient.
>
> > 3.  LOOP forms are more easily modified and expanded
>
> I don't know how you define `ease of modification'.  I've never found
> mapping constructs that hard to modify.
>
> > Are these the main reasons, or are there others?  Any to the contrary?
>
> My primary objection to LOOP is that it is extremely difficult to write
> meta-level code that can manipulate loop expressions.  A MAP expression
> is just a function call, but LOOP requires a serious state-machine to
> parse.  You can macro-expand the LOOP away, but the resulting expansion
> is almost as hard to understand as the original.
>
> My second objection to LOOP is that it operates at a low level of
> abstraction.  One very frequent use of iteration is when you wish to
> apply an operation to a collection of objects.  You shouldn't have to
> specify how the collection is traversed, and you certainly shouldn't
> have to specify a serial traversal algorithm if it doesn't matter.
> LOOP intertwines the operation you wish to perform with the minutiae of
> traversal --- finding an initial element, stepping to the next element,
> stopping when done.  I don't care about these details.  I don't care if
> the program LOOPs, uses recursion, farms out the work to other
> processors, or whatever, so long as it produces the answer I want.

Why does nearly no one use iterate ? I believe it is, compared to loop,
much more readable, concise and powerfull and extendable, and it is
apparently rarely used. Is there a reason for that ?  
Frank
From: Pascal Costanza
Subject: Re: ITERATE vs LOOP - Popular opinion observation?
Date: 
Message-ID: <4jfa6aF7o33aU1@individual.net>
franks wrote:
> Joe Marshall wrote:

>> My primary objection to LOOP is that it is extremely difficult to write
>> meta-level code that can manipulate loop expressions.  A MAP expression
>> is just a function call, but LOOP requires a serious state-machine to
>> parse.  You can macro-expand the LOOP away, but the resulting expansion
>> is almost as hard to understand as the original.
>>
>> My second objection to LOOP is that it operates at a low level of
>> abstraction.  One very frequent use of iteration is when you wish to
>> apply an operation to a collection of objects.  You shouldn't have to
>> specify how the collection is traversed, and you certainly shouldn't
>> have to specify a serial traversal algorithm if it doesn't matter.
>> LOOP intertwines the operation you wish to perform with the minutiae of
>> traversal --- finding an initial element, stepping to the next element,
>> stopping when done.  I don't care about these details.  I don't care if
>> the program LOOPs, uses recursion, farms out the work to other
>> processors, or whatever, so long as it produces the answer I want.
> 
> Why does nearly no one use iterate ? I believe it is, compared to loop,
> much more readable, concise and powerfull and extendable, and it is
> apparently rarely used. Is there a reason for that ?  

iterate doesn't address Joe's objections. It's still an iteration 
construct and not a declarative one.

List comprehensions, or similar declarative abstractions, would address 
them.

Indeed that's how I try to use LOOP. When I say this:

(loop for elem in list
       when (some-predicate elem)
       collect elem)

...I try not to understand this as an iteration, but as a declarative 
expression of the result I want. More often than not, this works 
sufficiently well, but Joe is right that LOOP is at least sometimes not 
optimal in this regard. However, IMHO, as a poor man's list 
comprehension facility it's mostly good enough.

The prime advantage of LOOP is that it's part of ANSI Common Lisp and so 
available by default everywhere.


Pascal

-- 
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/
From: Rob Warnock
Subject: Re: ITERATE vs LOOP - Popular opinion observation?
Date: 
Message-ID: <G7qdnSkkR5E8T0_ZnZ2dnUVZ_oidnZ2d@speakeasy.net>
Pascal Costanza  <··@p-cos.net> wrote:
+---------------
| List comprehensions, or similar declarative abstractions, would address 
| them.  Indeed that's how I try to use LOOP. When I say this:
|     (loop for elem in list
|            when (some-predicate elem)
|            collect elem)
| ...I try not to understand this as an iteration, but as a declarative 
| expression of the result I want.
+---------------

And to my taste, that's *much* cleaner looking and
more comprehensible [pardon the pun!] six months later
than anything I can think of writing with MAPxxx.

The combination of LOOP...WHEN...COLLECT work particularly
well together, especially when there are multiple WHEN...COLLECT
sub-expressions. Here's an admittedly-ugly [but useful] piece
of code. Would a MAPxxx version be more readable?

    ;;; Spray bits out for easy reading of hardware registers
    (defun decode-bits (n)
      (let ((mflag (minusp n)))
	(when mflag
	  (setf n (lognot n)))
	(loop for i downfrom (integer-length n) to 0
	  when mflag
	    collect (if (zerop n) 'all-ones 'all-ones-except)
	    and do (setf mflag nil)
	  when (logbitp i n)
	    collect i)))

Usage:

    > (decode-bits 16542)

    (14 7 4 3 2 1)
    > (decode-bits -16542)

    (ALL-ONES-EXCEPT 14 7 4 3 2 0)
    > (decode-bits -1)

    (ALL-ONES)
    > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Lars Brinkhoff
Subject: Re: ITERATE vs LOOP - Popular opinion observation?
Date: 
Message-ID: <857j1px9vl.fsf@junk.nocrew.org>
····@rpw3.org (Rob Warnock) writes:
> The combination of LOOP...WHEN...COLLECT work particularly well
> together, especially when there are multiple WHEN...COLLECT
> sub-expressions.  Here's an admittedly-ugly [but useful] piece of
> code.
>
>     (defun decode-bits (n)
>       (let ((mflag (minusp n)))
> 	(when mflag
> 	  (setf n (lognot n)))
> 	(loop for i downfrom (integer-length n) to 0
> 	  when mflag
> 	    collect (if (zerop n) 'all-ones 'all-ones-except)
> 	    and do (setf mflag nil)
> 	  when (logbitp i n)
> 	    collect i)))

I think I would like to rearrange the logic slightly:

  (defun decode-bits (n)
    (cond
      ((eql n -1) '(all-ones))
      ((minusp n) (cons 'all-ones-except (decode-bits (lognot n))))
      (t
       (loop for i downfrom (integer-length n) to 0
             when (logbitp i n)
               collect i))))

> Would a MAPxxx version be more readable?

Starting from this later version, and assuming some kind of MAP-BITS
function, I don't think it would too bad.  But in this case, a mapping
operation may not be suitable.  You don't want to map the bits in one
integer to some other bits in a resulting integer.
From: Rob Warnock
Subject: Re: ITERATE vs LOOP - Popular opinion observation?
Date: 
Message-ID: <QZGdnZ01p4zrZ0_ZnZ2dnUVZ_tqdnZ2d@speakeasy.net>
Lars Brinkhoff  <·········@nocrew.org> wrote:
+---------------
| I think I would like to rearrange the logic slightly:
|   (defun decode-bits (n)
|     (cond
|       ((eql n -1) '(all-ones))
|       ((minusp n) (cons 'all-ones-except (decode-bits (lognot n))))
|       (t
|        (loop for i downfrom (integer-length n) to 0
|              when (logbitp i n)
|                collect i))))
+---------------

Yeah, that is much cleaner, thanks!!

+---------------
| > Would a MAPxxx version be more readable?
| 
| Starting from this later version, and assuming some kind of MAP-BITS
| function, I don't think it would too bad.
+---------------

Well, the guts of it is still the LOOP...WHEN...COLLECT idiom,
and that's going to need a hairy MAPCAN or something to get
equivalent behavior. Not to mention that COLLECT is generally much
more efficient that the (REVERSE (DOxxx ... (WHEN ... (PUSH ...))))
idiom.


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Lars Brinkhoff
Subject: Re: ITERATE vs LOOP - Popular opinion observation?
Date: 
Message-ID: <85k65owr4o.fsf@junk.nocrew.org>
····@rpw3.org (Rob Warnock) writes:
> Lars Brinkhoff  <·········@nocrew.org> wrote:
> > (loop for i downfrom (integer-length n) to 0
> >       when (logbitp i n)
> >         collect i)
> > > Would a MAPxxx version be more readable?
> > Starting from this later version, and assuming some kind of MAP-BITS
> > function, I don't think it would too bad.
> Well, the guts of it is still the LOOP...WHEN...COLLECT idiom,
> and that's going to need a hairy MAPCAN or something to get
> equivalent behavior.

I said "assuming some kind of MAP-BITS", which would let me write

  (let ((bits nil))
    (map-bits/index (lambda (bit i)
                      (when bit
                        (push i bits)))
                    n)
    bits)

While I wouldn't necessarily say this is "more readable", I still
don't think it's "too bad".

If MAP-BITS/INDEX is too much of a stretch (why, it should be in every
bit-manipulating programmer's toolbox!), you could imagine a more
general version operating on an integer exploded into a sequence of
bits.  (For now, we're more interested in readability than efficiency,
right?)

  (reverse
    (filter/index (lambda (bit i)
                    (if bit
                        i                 ;Collect this.
                        (values)))        ;Nothing to collect.
                  (integer-bit-vector n)))

I'm still not advocating this style of programming, just trying to
provide alternatives to hairy old MAPCAN idioms etc.

> Not to mention that COLLECT is generally much more efficient that
> the (REVERSE (DOxxx ... (WHEN ... (PUSH ...)))) idiom.

Agreed.  When not using LOOP, I prefer to use some kind of
WITH-COLLECTOR macro.
From: Nathan Baum
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <Pine.LNX.4.64.0608032314430.24883@localhost>
On Thu, 3 Aug 2006, Joe Marshall wrote:
>
> Jonathon McKitrick wrote:
>> It seems to me that popular opinion leans toward LOOP in favor of map
>> and variants where either would apply
>
> Allow me to be the token knee-jerk anti-loopist.  I hate LOOP.
>
>> , for the following reasons, to
>> mention a few:
>>
>> 1.  LOOP is more readable
>
> I don't know what you mean by readable.  It certainly sounds more like
> english when you read it out loud from left to right, but I find it to
> be less easy to predict what the resulting code will look like after it
> is expanded.

Why is that important?

>> 2.  LOOP is more efficient
>
> Than MAP?  Loop should be as efficient as the constructs it expands
> into.  There is no reason that MAP cannot be as efficient.
>
>> 3.  LOOP forms are more easily modified and expanded
>
> I don't know how you define `ease of modification'.  I've never found
> mapping constructs that hard to modify.

Suppose you have

   (loop for x in (get-list)
         do (format t "~A~%" x))

and then it turns out you need to print a numeric index. You can do

   (loop for x in (get-list)
         for i from 0
         do (format t "~A - ~A~%" i x))

If you start with

   (mapc (lambda (x) (format t "~A~%" x)) (get-list))

it seems (to me) that it'd be harder to modify it as needed,

   (let ((list (get-list)))
     (mapc (lambda (i x) (format t "~A - ~A" i x))
           (range 0 (length list))
           list))

(I'm assuming the toolkit includes a RANGE utility, or something similar.)

Then suppose you later need the loop/map to collect some of the values 
under certain conditions. You might have

   (loop for x in (get-list)
         for i from 0
         do (format t "~A - ~A~%" i x)
         if (test x)
         collect (foo x))

compared to

   (let ((list (get-list)))
     (mapcan (lambda (i x)
               (format t "~A - ~A" i x)
               (if (test x)
                   (list (foo x))
                 nil))
              (range 0 (length list))
              list))

Not only did I not have to change as much code to make the LOOP do what I 
wanted, it's also obvious (to me) that the LOOP does what I intend, whilst 
I'd have to spend a little extra time testing the MAPCAN to make sure it's 
really doing what I think it would.

>> Are these the main reasons, or are there others?  Any to the contrary?
>
> My primary objection to LOOP is that it is extremely difficult to write
> meta-level code that can manipulate loop expressions.  A MAP expression
> is just a function call, but LOOP requires a serious state-machine to
> parse.  You can macro-expand the LOOP away, but the resulting expansion
> is almost as hard to understand as the original.

The solution to this seems to be not to use LOOP in code which is 
destined for transformation.

> My second objection to LOOP is that it operates at a low level of
> abstraction.  One very frequent use of iteration is when you wish to
> apply an operation to a collection of objects.  You shouldn't have to
> specify how the collection is traversed, and you certainly shouldn't
> have to specify a serial traversal algorithm if it doesn't matter.
> LOOP intertwines the operation you wish to perform with the minutiae of
> traversal --- finding an initial element, stepping to the next element,
> stopping when done.

I'm pretty sure it doesn't.

   (loop for x in list do (format t "~A: ~A~%" x (foo x)))

doesn't seem any more concerned with the mechanics of the traversal than

   (mapc (lambda (x) (format t "~A: ~A~%" x (foo x))) list)

Certainly it's *possible* to write a LOOP which manually deals with CARs 
and CDRs. But you don't *have* to, and unless it matters, I don't.

> I don't care about these details.  I don't care if the program LOOPs, 
> uses recursion, farms out the work to other processors, or whatever, so 
> long as it produces the answer I want.
From: Joe Marshall
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154716860.694346.63520@p79g2000cwp.googlegroups.com>
Nathan Baum wrote:
> On Thu, 3 Aug 2006, Joe Marshall wrote:
> >
> > I don't know what you mean by readable.  It certainly sounds more like
> > english when you read it out loud from left to right, but I find it to
> > be less easy to predict what the resulting code will look like after it
> > is expanded.
>
> Why is that important?

I understand code by considering alternatives to what I write.  It
isn't obvious to me what parts of a loop end up where in the result and
what parts are nested in what other parts.

> > I don't know how you define `ease of modification'.  I've never found
> > mapping constructs that hard to modify.
>
> Suppose you have
>
>    (loop for x in (get-list)
>          do (format t "~A~%" x))
>
> and then it turns out you need to print a numeric index. You can do
>
>    (loop for x in (get-list)
>          for i from 0
>          do (format t "~A - ~A~%" i x))
>
> If you start with
>
>    (mapc (lambda (x) (format t "~A~%" x)) (get-list))
>
> it seems (to me) that it'd be harder to modify it as needed,
>
>    (let ((list (get-list)))
>      (mapc (lambda (i x) (format t "~A - ~A" i x))
>            (range 0 (length list))
>            list))
>
> (I'm assuming the toolkit includes a RANGE utility, or something similar.)
>
> Then suppose you later need the loop/map to collect some of the values
> under certain conditions. You might have
>
>    (loop for x in (get-list)
>          for i from 0
>          do (format t "~A - ~A~%" i x)
>          if (test x)
>          collect (foo x))
>
> compared to
>
>    (let ((list (get-list)))
>      (mapcan (lambda (i x)
>                (format t "~A - ~A" i x)
>                (if (test x)
>                    (list (foo x))
>                  nil))
>               (range 0 (length list))
>               list))
>
> Not only did I not have to change as much code to make the LOOP do what I
> wanted, it's also obvious (to me) that the LOOP does what I intend, whilst
> I'd have to spend a little extra time testing the MAPCAN to make sure it's
> really doing what I think it would.

I suppose in these cases LOOP is easier to modify.

> >> Are these the main reasons, or are there others?  Any to the contrary?
> >
> > My primary objection to LOOP is that it is extremely difficult to write
> > meta-level code that can manipulate loop expressions.  A MAP expression
> > is just a function call, but LOOP requires a serious state-machine to
> > parse.  You can macro-expand the LOOP away, but the resulting expansion
> > is almost as hard to understand as the original.
>
> The solution to this seems to be not to use LOOP in code which is
> destined for transformation.

If I don't use LOOP at all, this is easy.
From: Ken Tilton
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <0BPAg.1025$3U4.539@fe12.lga>
Joe Marshall wrote:
> Jonathon McKitrick wrote:
> 
>>It seems to me that popular opinion leans toward LOOP in favor of map
>>and variants where either would apply
> 
> 
> Allow me to be the token knee-jerk anti-loopist.  I hate LOOP.
> 
> 
>>, for the following reasons, to
>>mention a few:
>>
>>1.  LOOP is more readable
> 
> 
> I don't know what you mean by readable.

No code is readable. But LOOP is more writeable. Anyway...

>>2.  LOOP is more efficient
> 
> 
> Than MAP?  Loop should be as efficient as the constructs it expands
> into.  There is no reason that MAP cannot be as efficient.

Well, I think the point is that LOOP automates things like collecting 
and does so efficiently saving me the effort.


> 
> My primary objection to LOOP is that it is extremely difficult to write
> meta-level code that can manipulate loop expressions.

That's a good one. I myself have never reached that particular 
meta-level, but then I am just a simple application programmer.


> My second objection to LOOP is that it operates at a low level of
> abstraction.  One very frequent use of iteration is when you wish to
> apply an operation to a collection of objects.  You shouldn't have to
> specify how the collection is traversed,

You mean: (loop for item in list...)? Hmmmm.

But now that you mention it, of course the brilliant thing about loop is 
that you can do destructuring: (loop (for (thing1 thing2) in list...

And i can use WITH for any LETs I would normally need. And I must say my 
LOOPS tend to be either very simple and obvious (and then I do not have 
to type l-a-m-b-d-a!) or very hairy in which case in one form (however 
un-lispy and un-meta-parseable) I can get a whole lotta stuff written 
that would not appear so self-contained (which maybe gets to the 
readability/maintainability points).

In the end it comes back to my first point: you cannot compare MAP and 
LOOP, when one is using the full power of LOOP and its DSL quality is 
apparent (at which point the equivalent loop-less lisp is to a degree 
like writing assembler instead of an HLL.

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Pascal Costanza
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <4jit5sF860edU1@individual.net>
Ken Tilton wrote:
> 
> 
> Joe Marshall wrote:
>>
>> My primary objection to LOOP is that it is extremely difficult to write
>> meta-level code that can manipulate loop expressions.
> 
> That's a good one. I myself have never reached that particular 
> meta-level, but then I am just a simple application programmer.

You don't write macros?!?


Pascal

-- 
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/
From: Ken Tilton
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <wfZAg.249$Fs3.144@fe09.lga>
Pascal Costanza wrote:
> Ken Tilton wrote:
> 
>>
>>
>> Joe Marshall wrote:
>>
>>>
>>> My primary objection to LOOP is that it is extremely difficult to write
>>> meta-level code that can manipulate loop expressions.
>>
>>
>> That's a good one. I myself have never reached that particular 
>> meta-level, but then I am just a simple application programmer.
> 
> 
> You don't write macros?!?

One of us did not understand Mr. Marshall.

:)

ken

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Pascal Costanza
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <4jdrokF7heonU1@individual.net>
Jonathon McKitrick wrote:
> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:
> 
> 1.  LOOP is more readable
> 2.  LOOP is more efficient
> 3.  LOOP forms are more easily modified and expanded
> 
> Are these the main reasons, or are there others?  Any to the contrary?

mapcar and mapc are pretty useful when the function you want to map is 
already defined (or needs to be defined) anyway, because (mapcar 
#'foobar some-list) or (mapc #'foobar some-list) is pretty concise. 
However, when it gets more complicated than that, I tend to switch to LOOP:

- When you have to build an anonymous function for mapcar, LOOP 
typically uses the same number of source code lines, and seems clearer 
to me. Compare:

(mapcar (lambda (x) ... do something with x ...)
         some-list)

(loop for x in some-list
       collect ... do something with x ...)

- I don't remember having used functions with more than one argument in 
conjunction with mapcar, so it seems to me that (mapcar #'foobar 
some-list1 some-list2) [or even more lists] is pretty uncommon. Again, 
if you need to build an anonymous function with more than one arguments, 
LOOP seems clearer to me:

(mapc (lambda (x y) ...)
       some-list1 some-list2)

(loop for x in some-list1
       for y in some-list2
       do ...)

- More often than not, the different variables actually iterate over 
different kinds of values. Recently, I needed the following costruct 
quite often:

(loop for x in some-list
       for i from 0
       collect `(,x ,i))

This enumerates all elements in a list. You would have to express this 
completely manually without LOOP because none of the mapxyz functions 
help you here.

- The mapcan idiom to filter out certain elements is, IMHO, a hack. Compare:

(mapcan (lambda (x)
           (when (some-property-p x)
             (list x)))
         some-list)

(loop for x in some-list
       when (some-property-p x)
       collect x)

In this case, LOOP is more concise, and more importantly more explicit 
about what it actually does. To me, that's in fact the most important 
advantage of LOOP - it typically tells you what it does in a way that is 
almost close to natural language.


Pascal

-- 
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/
From: Lars Brinkhoff
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <851wryyuxj.fsf@junk.nocrew.org>
Pascal Costanza <··@p-cos.net> writes:
> I don't remember having used functions with more than one argument
> in conjunction with mapcar, so it seems to me that (mapcar #'foobar
> some-list1 some-list2) is pretty uncommon.

In one of my programs 2% of the calls to mapcar used more than one
list argument.  E.g:

   (mapcar #'list syms '#1=((gensym) . #1#))

That seems quite concise (if perhaps not entirely crystal clear, in
this case) to me.

> - The mapcan idiom to filter out certain elements is, IMHO, a hack. Compare:
>
> (mapcan (lambda (x)
>            (when (some-property-p x)
>              (list x)))
>          some-list)
>
> (loop for x in some-list
>        when (some-property-p x)
>        collect x)

I would think that remove-if-not (or remove-if) is usually better than
this mapcan idiom.  Maybe even competetive with loop.

(Not arguing against loop.  I like loop.)
From: Tayssir John Gabbour
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154611060.474418.258120@m79g2000cwm.googlegroups.com>
Lars Brinkhoff wrote:
> Pascal Costanza <··@p-cos.net> writes:
> > (mapcan (lambda (x)
> >            (when (some-property-p x)
> >              (list x)))
> >          some-list)
> >
> > (loop for x in some-list
> >        when (some-property-p x)
> >        collect x)
>
> I would think that remove-if-not (or remove-if) is usually better than
> this mapcan idiom.  Maybe even competetive with loop.
>
> (Not arguing against loop.  I like loop.)

I sometimes think remove-if-not would be used even more if named
something like 'keep', or SICP's 'filter'. Because there's something
double negative-ish about it.

Tayssir
From: ···············@yahoo.com
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154614729.299989.180090@b28g2000cwb.googlegroups.com>
Tayssir John Gabbour wrote:
> I sometimes think remove-if-not would be used even more if named
> something like 'keep', or SICP's 'filter'. Because there's something
> double negative-ish about it.
>
> Tayssir

I remember once seeing subset as a synonym for remove-if-not, and
subset-not for remove-if.  Was that Symbolics Lisp in the 1980s?
From: Pascal Costanza
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <4jece4F7l5o7U1@individual.net>
Tayssir John Gabbour wrote:
> Lars Brinkhoff wrote:
>> Pascal Costanza <··@p-cos.net> writes:
>>> (mapcan (lambda (x)
>>>            (when (some-property-p x)
>>>              (list x)))
>>>          some-list)
>>>
>>> (loop for x in some-list
>>>        when (some-property-p x)
>>>        collect x)
>> I would think that remove-if-not (or remove-if) is usually better than
>> this mapcan idiom.  Maybe even competetive with loop.
>>
>> (Not arguing against loop.  I like loop.)
> 
> I sometimes think remove-if-not would be used even more if named
> something like 'keep', or SICP's 'filter'. Because there's something
> double negative-ish about it.

Hmm, I rather think that remove-if-not is too unstable, in the following 
sense: When I say, for example, (mapcar #'car some-list) or (mapcar 
#'slot-definition-name some-slots) in my code, it's unlikely that this 
is going to change. However, when I say (remove-if-not #'some-property-p 
some-list), my guess is that it's more likely that I will touch that 
code again to modify the conditions, in the sense of refactoring my code 
when I better understand what it's supposed to be doing. The LOOP macro 
is more flexible in changing the code, while remove-if-not only allows 
certain changes.

But that's probably just guesswork. ;)


Pascal

-- 
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/
From: Timofei Shatrov
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <44d23b2c.35732260@news.readfreenews.net>
On 3 Aug 2006 06:17:40 -0700, "Tayssir John Gabbour"
<···········@yahoo.com> tried to confuse everyone with this message:

>Lars Brinkhoff wrote:
>> Pascal Costanza <··@p-cos.net> writes:
>> > (mapcan (lambda (x)
>> >            (when (some-property-p x)
>> >              (list x)))
>> >          some-list)
>> >
>> > (loop for x in some-list
>> >        when (some-property-p x)
>> >        collect x)
>>
>> I would think that remove-if-not (or remove-if) is usually better than
>> this mapcan idiom.  Maybe even competetive with loop.
>>
>> (Not arguing against loop.  I like loop.)
>
>I sometimes think remove-if-not would be used even more if named
>something like 'keep', or SICP's 'filter'. Because there's something
>double negative-ish about it.
>
>Tayssir
>

Being deprecated by ANSI standard doesn't help it either.

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|
From: Rob Warnock
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <N9CdnZeJ0pv9V0_ZnZ2dnUVZ_v2dnZ2d@speakeasy.net>
Timofei Shatrov <····@mail.ru> wrote:
+---------------
| <···········@yahoo.com> tried to confuse everyone with this message:
| >I sometimes think remove-if-not would be used even more if named
| >something like 'keep', or SICP's 'filter'. Because there's something
| >double negative-ish about it.
| 
| Being deprecated by ANSI standard doesn't help it either.
+---------------

But don't forget that all of the "deprecated" features in
CLHS are still *required* to be present in an implementation
that purports to be a "Common Lisp".

    1.8 Deprecated Language Features

    Deprecated language features are not expected to appear
    in future Common Lisp tandards, but are required to
    be implemented for conformance with this standard; see
    Section 1.5.1.1 (Required Language Features).


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Tayssir John Gabbour
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154682440.084788.316000@s13g2000cwa.googlegroups.com>
Rob Warnock wrote:
> Timofei Shatrov <····@mail.ru> wrote:
> | <···········@yahoo.com> tried to confuse everyone with this message:
> | >I sometimes think remove-if-not would be used even more if named
> | >something like 'keep', or SICP's 'filter'. Because there's something
> | >double negative-ish about it.
> |
> | Being deprecated by ANSI standard doesn't help it either.
>
> But don't forget that all of the "deprecated" features in
> CLHS are still *required* to be present in an implementation
> that purports to be a "Common Lisp".

My understanding is that the deprecation was informally deprecated. ;)

Kent Pitman wrote:

<···············@shell01.TheWorld.com>
> I think it's generally accepted in the community that we made a mistake
> deprecating those.
>
> You should use :test (complement test) rather than :test-not test.
> But for the -if-not functions, just use them and ignore the deprecation.
http://groups.google.com/group/comp.lang.lisp/msg/60b5bda8223abaf2


IIRC, Peter Seibel's PCL guesstimated that remove-if-not is probably
used more (or at least is more useful) than remove.


Tayssir
From: Stefan Mandl
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <4jff92F7rej2U1@news.dfncis.de>
> - More often than not, the different variables actually iterate over 
> different kinds of values. Recently, I needed the following costruct 
> quite often:
> 
> (loop for x in some-list
>       for i from 0
>       collect `(,x ,i))
> 

Hey Pascal, how about

(reverse (maplist #'(lambda (l) `(,(first l) ,(- (length l) 1))) (reverse some-list)))

.. but .. hmm no, that's way to inefficient and plain ugly .. your are right ;)
From: mac
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154639302.474553.105820@p79g2000cwp.googlegroups.com>
Jonathon McKitrick wrote:
> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:
>
> 1.  LOOP is more readable
> 2.  LOOP is more efficient
> 3.  LOOP forms are more easily modified and expanded
>
> Are these the main reasons, or are there others?  Any to the contrary?

To tell you the truth, it really doesn't matter, use whatever construct
that make your code as clear as possible in the context.

I found that do is hard to read. dolist and dotimes are mostly good for
simple iteration. Like kenny mentioned, unless I have a named function
to pass to map-xxx, I'd rather use loop instead of (map-xxx
(lambda)...) because the code will be less verbose.

Actually iterate is better than loop technically (you can use arbitrary
lisp code for conditional branching, whereas with loop you can only use
the built-in keyword, and lots of cool constructs like generators, etc
etc)

For this reason I procrastined to learn the loop syntax initially after
I found iterate.

Iterate's documentation is excellent, it can do everything that loop
can do and more, and I learn all the features after reading the manual.
Much shorter time it took me to learn loop.

But eventually I still need to master the loop sytnax, because
everybody else is using it and in reality very few people use iterate.
It's very counter productive if I have to stop and think about what it
does whenever I see a loop construct.

Now I prefer to use loop whenever possible, and only use iterate after
I tried really hard and cannot find a elegant solution with loop.

It's sad, it's the same reason why lisp is not popular (and will never
be) even it's the best high level programming language ever invented.
From: Ken Tilton
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <C8VAg.107$9T2.58@fe10.lga>
mac wrote:

> It's sad, it's the same reason ...

worse is better...

>...why lisp is not popular (and will never
> be)

If you have a crystal ball so reliable that you will bet the ranch 
against the best language catching on in a world still in turmoil and 
moving ever closer to Lisp's features, get the fuck off c.l.l and into 
the penny markets.

hth, kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: ········@gmail.com
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154672714.038460.219120@p79g2000cwp.googlegroups.com>
Jonathon McKitrick wrote:
> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:

I would like to add a third item to this poll: when, if ever, do you
think series (or some similar approach) could be preferable to those
above. It is (if I understand correctly) another iteration mechanism
that almost got to a standard a time ago, it looks quite elegant, but I
dont think I have ever seen it used in recent public code (and,
honestly, when I tried to use it, it seemed to be too compilcated for
casual use)

Tomas
From: Joe Marshall
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154718545.804122.233900@p79g2000cwp.googlegroups.com>
········@gmail.com wrote:
>
> I would like to add a third item to this poll: when, if ever, do you
> think series (or some similar approach) could be preferable to those
> above. It is (if I understand correctly) another iteration mechanism
> that almost got to a standard a time ago, it looks quite elegant, but I
> dont think I have ever seen it used in recent public code (and,
> honestly, when I tried to use it, it seemed to be too compilcated for
> casual use)

I've used series.  In the cases where it works, it works well, but in
other cases it can be really nasty.  For example, I have some code that
manipulates strings in various formats.  One format it handles is
simple-arrays of (unsigned-byte 8) which contain the code-points of a
string encoded in UTF-8.  When I want to print one of these, I use this
code:

(collect-stream
   stream
   (#m code-char
       (ucs-4->ucs-2
        (utf-8->ucs-4
         (scan 'simple-vector-8b code-unit-vector))))
   #'write-char)

The underlying lisp uses UCS-2 (16-bit old-style unicode) and I'm
ignoring surrogates.

As you can see, this code is *really* easy to understand:  You scan
across the code units, transcode them from utf-8 to ucs-4, transcode
again to ucs-2, convert to characters, and write them to the stream.  I
could have written a utf-8->ucs-2 converter, but it didn't seem worth
it.

There is a catch.  The code for utf-8->ucs-4 is *horrendous*.  The
series code likes to work in `lock step' mode: each input quantum
should produce exactly one output quantum.  When things are in lock
step, the series code can unroll all the mapping into a single loop.
Unfortunately, utf8 encoding uses from one to six bytes to encode a
32-bit value, so we violate the lock step condition early on in the
process.

Fortunately, the series code can handle a *little* bit of asynchronous
code provided you meet some complicated restrictions.  With a bit of
trickery, you can get the utf8 decoding to be close enough to lock step
that rest of the code will optimize.

I really like how series makes some code extremely clean, but it comes
at the cost of making some code even more extremely messy.
From: Steven E. Harris
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <83wt9n568u.fsf@torus.sehlabs.com>
Joe Marshall <··········@gmail.com> writes:

> With a bit of trickery, you can get the utf8 decoding to be close
> enough to lock step that rest of the code will optimize.

Joe had been kind enough to explain the gory details in the thread
"Generators in Lisp" from June 2004.�


Footnotes: 
� http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/1b8d5d7c129aa259/a38d231b39f48e34?#a38d231b39f48e34

-- 
Steven E. Harris
From: Jonathon McKitrick
Subject: Re: MAP (and variants) - What is MAPLIST  useful for?
Date: 
Message-ID: <1154694166.276833.270430@m73g2000cwd.googlegroups.com>
When is it really useful to iterate over a list, then the cdr of the
list again, the the cddr... etc?

I just can't come up with a non-contrived example.
From: ········@gmail.com
Subject: Re: MAP (and variants) - What is MAPLIST useful for?
Date: 
Message-ID: <1154721098.858464.176210@b28g2000cwb.googlegroups.com>
Jonathon McKitrick wrote:
> When is it really useful to iterate over a list, then the cdr of the
> list again, the the cddr... etc?
>
> I just can't come up with a non-contrived example.

I don't know about MAPLIST, but I've used MAPL to iterate over the
elements of a list when I needed to pass what the remaining elements of
the list are to the operation  on the elements...

Also wouldn't you need MAPL to impliment something like MEMBER?

Nick
From: Timofei Shatrov
Subject: Re: MAP (and variants) - What is MAPLIST  useful for?
Date: 
Message-ID: <44d34d00.19208386@news.readfreenews.net>
On 4 Aug 2006 05:22:46 -0700, "Jonathon McKitrick"
<···········@bigfoot.com> tried to confuse everyone with this message:

>When is it really useful to iterate over a list, then the cdr of the
>list again, the the cddr... etc?
>
>I just can't come up with a non-contrived example.
>

(loop for x on list do ...)

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|
From: Timofei Shatrov
Subject: Re: MAP (and variants) - What is MAPLIST  useful for?
Date: 
Message-ID: <44d37f40.32074168@news.readfreenews.net>
On Fri, 04 Aug 2006 13:35:42 GMT, ····@mail.ru (Timofei Shatrov) tried
to confuse everyone with this message:

>On 4 Aug 2006 05:22:46 -0700, "Jonathon McKitrick"
><···········@bigfoot.com> tried to confuse everyone with this message:
>
>>When is it really useful to iterate over a list, then the cdr of the
>>list again, the the cddr... etc?
>>
>>I just can't come up with a non-contrived example.
>>
>
>(loop for x on list do ...)
>

Oops - I was thinking that this is the continuation of MAP* vs LOOP
debate... Anyway, I used (loop for ... on ...) several times, but this
loop either exited early (allowing to get the tail quickly) or I used
"by #'cddr" to get at two consecutive elements. Both of these uses are
impractical with MAPLIST.

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|
From: Frank Buss
Subject: Re: MAP (and variants) - What is MAPLIST  useful for?
Date: 
Message-ID: <s4me89tvxiwe.fmua4naixw58$.dlg@40tude.net>
Jonathon McKitrick wrote:

> When is it really useful to iterate over a list, then the cdr of the
> list again, the the cddr... etc?
> 
> I just can't come up with a non-contrived example.

a quick hack:

(defun all-combinations (list)
  (let ((combinations '()))
    (maplist (lambda (cdr)
               (let ((car (car cdr)))
                 (mapcar (lambda (element)
                           (push (list car element) combinations))
                         cdr)))
             list)
    combinations))

CL-USER > (all-combinations '(1 2 3 4))
((4 4) (3 4) (3 3) (2 4) (2 3) (2 2) (1 4) (1 3) (1 2) (1 1))

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Jonathon McKitrick
Subject: Re: MAP (and variants) vs LOOP - here's an example....
Date: 
Message-ID: <1154761550.941227.89180@m79g2000cwm.googlegroups.com>
Here's a real-world problem I'm working on:

I have one target array and 5 reference arrays, all with just a few
dozen elements and all the same size/type.  I want to compare the
target array to each of the reference arrays simply by summing the
absolute value of the difference between target array element x and
reference array element x, for all elements.

Originally, all I returned was the index of the array most closely
matching the target.
Note: 'rpt' is an object with a 'target-array' slot.

(defun fit-profiles (rpt)
  (let (total-deltas)
    (dotimes (i 5)
      (let ((profile-id (search-profiles (format nil "profile~A" (1+
i)))))
	(push
	 (reduce #'+ (mapcar #'(lambda (a b) (abs (- a b))) (target-array rpt)
(get-reference-array profile-id)))
	 total-deltas)))
    (1+ (position (apply #'min total-deltas) (nreverse
total-deltas))))))

In the next round, I am not only interested in the closest match, but
the next closest match as well.  So now I will return not only the
closest match, but total-deltas also.

I started writing this with LOOP, but the nature of calculating the
deltas seemed fit so well with mapcar, why bother?  But maybe I'm
overlooking a better way, especially since returning the list of deltas
results in an ugly (setf total-deltas (nreverse total-deltas)) which
LOOP could avoid with COLLECT.  I'm also allowing for the possibility
there is a more elegant solution using neither LOOP nor MAPCAR that I
haven't found yet either.
From: lispolos
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154762932.399051.155000@i3g2000cwc.googlegroups.com>
Jonathon McKitrick wrote:
> Are these the main reasons, or are there others?  Any to the contrary?

Loop is only a language addition anybody is free to like or dislike, it
is certainly no distinctive "feature" of Lisp.

I love loop, in exactly one situation: a simple forever loop with no
loop clauses.

For all other situations I prefer a way where I can read exactly how it
will be done.

(BTW, it's more or less the same with regular expressions: in Lisp you
can do surprisingly often without them, beeing able to also see exactly
how it is done, where in many other languages it would be very
difficult to code without, and how it is done is more or less hidden by
the regular expression...)

Paul
From: Jonathon McKitrick
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154797381.635468.249400@p79g2000cwp.googlegroups.com>
lispolos wrote:
> (BTW, it's more or less the same with regular expressions: in Lisp you
> can do surprisingly often without them, beeing able to also see exactly
> how it is done, where in many other languages it would be very
> difficult to code without, and how it is done is more or less hidden by
> the regular expression...)

Any examples of this you can offer?
From: Rob Warnock
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <rNidnSSnH5RtwUjZnZ2dnUVZ_vKdnZ2d@speakeasy.net>
Jonathon McKitrick <···········@bigfoot.com> wrote:
+---------------
| lispolos wrote:
| > (BTW, it's more or less the same with regular expressions: in Lisp you
| > can do surprisingly often without them, beeing able to also see exactly
| > how it is done, where in many other languages it would be very
| > difficult to code without, and how it is done is more or less hidden by
| > the regular expression...)
| 
| Any examples of this you can offer?
+---------------

The following hack [with some names obfuscated] parses ".objd" files
[output from "objdump"] to collect a map of places in the target
code being debugged where the function "foo_trace()" was called.
[The function "foo_trace()" sticks its return address into a trace
buffer along with its other args, which is why this is useful.]

    (defun load-trace-map (&key (file "home:build/test/foo/foo.objd")
                                (verbose nil))
      (with-open-file (s file)
        (setf *trace-map* (make-hash-table))    ; clear out old stuff
        (loop with labels = 0
              for line = (read-line s nil nil)
              while line
          when (and (> (length line) 10)
                    (not (mismatch line "0002" :end1 4))
                    (mismatch "foo_trace" line :start2 10 :end2 22)
                    (eql 0 (mismatch "<foo_trace>" line :from-end t)))
            do (let* ((addr (parse-integer line :start 4 :end 8 :radix 16))
                      (end (position #\> line))
                      (label (subseq line 10 end)))
                 (when verbose
                   (format t "~(0x~4,'0x~) ~a~%" addr label))
                 (incf labels)
                 (setf (gethash addr *trace-map*) label))
          finally (format t "~d labels extracted~%" labels)))
      (values))
       
That is, given ".objd" lines like these:

    ...
    000217d4 <gorp+0x34> e1a01002 mov     r1, r2
    000217d8 <gorp+0x38> ebfffbc0 bl      000206e0 <foo_trace>
    000217dc <gorp+0x3c> e5941000 ldr     r1, [r4]
    ...

it will do a (SETF (GETHASH #x17d8 *TRACE-MAP*) "gorp+0x38").
So when the buffer is printed out, whenever the return address
#x17dc is seen in the trace buffer [which only stores the 16 LSBs],
the trace entry will be annotated with "gorp+0x38".


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Bill Atkins
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <1154798059.801434.292710@m73g2000cwd.googlegroups.com>
lispolos wrote:
> Jonathon McKitrick wrote:
> > Are these the main reasons, or are there others?  Any to the contrary?
>
> Loop is only a language addition anybody is free to like or dislike, it
> is certainly no distinctive "feature" of Lisp.

Sure it is.  What other language has anything like LOOP?  And what
other language would let the developer add LOOP if it didn't already
exist?
From: Friedrich Dominicus
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <87y7u3854z.fsf@flarge.here>
"Bill Atkins" <·········@gmail.com> writes:

> lispolos wrote:
>> Jonathon McKitrick wrote:
>> > Are these the main reasons, or are there others?  Any to the contrary?
>>
>> Loop is only a language addition anybody is free to like or dislike, it
>> is certainly no distinctive "feature" of Lisp.
>
> Sure it is.  What other language has anything like LOOP?  And what
> other language would let the developer add LOOP if it didn't already
> exist?
Tcl/Tk
and IO come to my mind immediatly.

I guess it could work in Ocaml also

Regards
Friedrich


-- 
Please remove just-for-news- to reply via e-mail.
From: Larry Elmore
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <b-KdnSaUS9bAZUnZnZ2dnUVZ_s-dnZ2d@comcast.com>
Friedrich Dominicus wrote:
> "Bill Atkins" <·········@gmail.com> writes:
> 
>> lispolos wrote:
>>> Jonathon McKitrick wrote:
>>>> Are these the main reasons, or are there others?  Any to the contrary?
>>> Loop is only a language addition anybody is free to like or dislike, it
>>> is certainly no distinctive "feature" of Lisp.
>> Sure it is.  What other language has anything like LOOP?  And what
>> other language would let the developer add LOOP if it didn't already
>> exist?
> Tcl/Tk
> and IO come to my mind immediatly.
> 
> I guess it could work in Ocaml also

Forth, certainly.

--Larry
From: Nathan Baum
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <Pine.LNX.4.64.0608052052530.21556@localhost>
On Sat, 5 Aug 2006, lispolos wrote:
>
> Jonathon McKitrick wrote:
>> Are these the main reasons, or are there others?  Any to the contrary?
>
> Loop is only a language addition anybody is free to like or dislike, it
> is certainly no distinctive "feature" of Lisp.
>
> I love loop, in exactly one situation: a simple forever loop with no
> loop clauses.
>
> For all other situations I prefer a way where I can read exactly how it
> will be done.
>
> (BTW, it's more or less the same with regular expressions: in Lisp you
> can do surprisingly often without them, beeing able to also see exactly
> how it is done, where in many other languages it would be very
> difficult to code without, and how it is done is more or less hidden by
> the regular expression...)

That's exactly *why* I use regular expressions. I don't care about *how* 
it's done, I just want to tell the program *what* should be done, and it 
makes it so. Apart from making things easier (IMO), it also means that 
improvements in the regular expression engine are automatically passed on 
to my code. Even though the *how* might change in the engine, the *what* 
I've specified in my code remains the same.

> Paul
From: Novus
Subject: Re: MAP (and variants) vs LOOP - Popular opinion observation?
Date: 
Message-ID: <2006080612410016807-novus@ngoqdeorg>
On 2006-08-02 19:17:40 -0400, "Jonathon McKitrick" 
<···········@bigfoot.com> said:

> It seems to me that popular opinion leans toward LOOP in favor of map
> and variants where either would apply, for the following reasons, to
> mention a few:
> 
> 1.  LOOP is more readable
> 2.  LOOP is more efficient
> 3.  LOOP forms are more easily modified and expanded
> 
> Are these the main reasons, or are there others?  Any to the contrary?

I prefer ITERATE over LOOP for the same reasons you list above and because
ITERATE forms actually look like lisp code.

Novus