Sujet : Re: simple loop question
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.lispDate : 25. Jun 2025, 02:16:05
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250624145614.845@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-06-24, B. Pym <
Nobody447095@here-nor-there.org> wrote:
Lars Brinkhoff wrote:
>
use LOOP to collect random integers into a list until the sum of that
list exceeds a constant (say 50).
(loop for x = (random 10) collect x sum x into y until (> y 50))
>
Gauche Scheme
>
(use srfi-27 :only (random-integer))
(define random random-integer)
>
Gauche lacks do*. We'll define a version that
has an extra feature that works well for this
problem.
>
(do* ((x 0 (random 10))
((y z) (0 ()) (+ cons)))
((> y 50) z))
>
Here's the line that uses the extra feature:
>
((y z) (0 ()) (+ cons)))
>
The two variables y and z are intialized to the
values 0 and '(), respectively.
For the next pass through the loop, each
variable is updated by a "kons" and the
variable in the line above this special line.
So this is equivalent to:
>
(y 0 (+ x y))
(z () (cons x z))
>
Using this feature, the do* solution is shorter than
the LOOP solution.
You don't seem to understand that if you use this do* only once for your
one-liner, it's not shorter? Everything you wrote yourself not coming
from the language contributes to the size of your solution.
Your definition is 41 nonblank lines of code, so your one liner is now actually
a 42 liner.
The macro has to be used 41 times to amortize that down to one line of
definition overhead per use, so that all 41 one-liners that use it are
effectively two-liners, carrying 100% overhead.
Say, if you think it's okay to write a 41 line definition that somehow doesn't
count toward the size of your one-liner, why wouldn't you just make that
definition a function, which then lets your one liner be something like this?
(r 10 50)
You could even hard code the parameters into the definition so
it's just (r); who says that the definition must be flexibly parametrized?
A Scheme wth symbol macros (standard CL feature) could get it down
to one symbol then.
(loop for x = (random 10) collect x sum x into y until (> y 50))
(do* ((x 0 (random 10)) ((y z) (0 ()) (+ cons))) ((> y 50) z))
The difference is negligible.
You don't seem to get that "collect x sum x into y until (> y 50)" is
easy for non-experts to understand. Maybe you have autism or something,
so you have no idea how neurotypicals process information.
You have three variables in your solution; x, y and z. Aren't you always
criticizing the use of comprehension constructs that use variables, instead of
point-free map/reduce type stuff?
Removing unnecessary spaces:
Removing spaces in a big decrement in the readability of the do*
code, yet places it at only a 7 character advantage.
(loop for x =(random 10)collect x sum x into y until(> y 50))
(do*((x 0(random 10))((y z)(0())(+ cons)))((> y 50)z))
Note that the order of the numbers in the list returned by do*
is reversed with respect to the order in which they were produced.
That could matter! Sometimes pseudo-random behavior is called upon to be
reproducible, and so you can't just replace code that lists random
numbers with code that does that in reverse order. You may have to
adjust test cases and possibly other things.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca