[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Loop

William James

1/25/2016 2:20:00 PM

Gareth McCaughan wrote:

> It's not a better algorithm. The point is that because it's
> more concise you are less likely to get lost in details, and
> therefore more likely to be able to spot algorithmic improvements.
> If you write
>
> (loop for x from a to b by d collect x)
>
> rather than
>
> (do ((j a (+ j d))
> (result nil))
> ((>= j b) (nreverse result))
> (push j result))
>
> then
>
> - it's about half as much code, and 1/4 as many lines with
> most people's indentation conventions;
>
> - it's therefore twice as fast to type
>
> - it takes quite a lot less than half as long to *write*
> (or at least it does for me; I found that I had to think
> for the best part of a second at a couple of places while
> writing the iterative version)
>
> - it's quicker to read

MatzLisp (Ruby):

0.step(9,2).to_a
===>
[0, 2, 4, 6, 8]

Another way:

(0..9).step(2).to_a

--
Elie [Wiesel] thus could have remained at Birkenau to await the Russians.
Although his father had permission to stay with him as a hospital patient or
orderly, father and son talked it over and decided to move out with the
Germans. --- Robert Faurisson