Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

.Re: Loop

6 views
Skip to first unread message

Robert L.

unread,
Jul 22, 2017, 6:24:31 AM7/22/17
to
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



* (loop for x from 20 to 30 by 2 collect x)

(20 22 24 26 28 30)



(use srfi-42)
(list-ec (: x 20 31 2) x)
===>
(20 22 24 26 28 30)



(use srfi-1)
(iota 6 20 2)
===>
(20 22 24 26 28 30)



(use list-comprehensions)
(range 20 31 2)
===>
(20 22 24 26 28 30)



(use traversal)
(map-linear abs 20 30 5)
===>
(20 22 24 26 28 30)




Let's make it more complicated.

* (loop for x from 20 to 30 by 2 collect (sqrt x))

(4.472136 4.690416 4.8989797 5.0990195 5.2915025 5.477226)

Not very precise.

(use srfi-1) ; unfold
(use holes) ; ##
(unfold ##(< 30 !!) sqrt ##(+ 2 !!) 20)
===>
(4.47213595499958 4.69041575982343 4.89897948556636 5.09901951359278
5.29150262212918 5.47722557505166)


(use list-comprehensions)
(for ((x 20 (+ x 2) (> x 30))) (sqrt x))
===>
(4.47213595499958 4.69041575982343 4.89897948556636 5.09901951359278
5.29150262212918 5.47722557505166)


--
I wrote until my fingers were blue, over and over: "Political
Correctness is a religion." I proved it and showed you why it is so
important that Political Correctness is a religion.
--- Bob Whitaker
0 new messages