Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #307: emissions from GSM-phones


comp / comp.lang.lisp / Re: Loop

SubjectAuthor
o Re: LoopB. Pym

1
Subject: Re: Loop
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sat, 7 Sep 2024 19:50 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Loop
Date: Sat, 7 Sep 2024 19:50:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 81
Message-ID: <vbiaq3$1gn18$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 07 Sep 2024 21:50:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="10cc50ea4be0dfeba3199ef500925cdf";
logging-data="1596456"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19T8lu1cOcITQJF9sQ07qq6"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:U5gtxJA76KXItd36lqON8oL6vLA=
View all headers

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))

Bad. Very bad.

(do ((x 20 (+ x 2))
(r '() (cons x r)))
((> x 30) (reverse r)))

===>
(20 22 24 26 28 30)

Shorter:

(do ((x 30 (- x 2))
(r '() (cons x r)))
((< x 20) r))

And is "nreverse" actually faster than "reverse"?

On page 222 in "ANSI Common Lisp" Paul Graham writes:

"In Lisp implementations with bad garbage collectors, programs that
cons a lot tend to run slowly. Until recently, most Lisp
implementations have had bad garbage collectors, and so it has become
a tradition that efficient programs should cons as little as possible.
Recent developments have turned this conventional wisdom on its head.
Some implementations now have such sophisticated garbage colletors
that it is faster to cons up new objects and throw them away than it
is to recycle them."

>
> 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

Testing:

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

(20 22 24 26 28 30)

Shorter:

Gauche Scheme

Function: unfold end-test key gen-next-seed seed

(use srfi-1) ;; unfold

;; pa$ is partial application (currying).
;; Using + for identity.
(unfold (pa$ < 30) + (pa$ + 2) 20)
===>
(20 22 24 26 28 30)

1

rocksolid light 0.9.8
clearnet tor