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

The LOOP macro

64 views
Skip to first unread message

Robert L.

unread,
Aug 19, 2017, 2:17:35 AM8/19/17
to
Peter Seibel wrote:

> (loop for x in things
> for foo-p = (foo-p x)
> for bar-p = (bar-p x)
> for baz-p = (baz-p x) ; added
> when foo-p collect x into foos
> when bar-p collect x into bars
> when baz-p collect x into bazes ; added
> when (and foo-p bar-p baz-p) collect x into both ; modified
> finally (return (values foos bars bazes both))) ; modified

(require data/queue)
(require srfi/42) ; do-ec

(let ((foos (make-queue)) (bars (make-queue)) (bazes (make-queue))
(all (make-queue)))
(do-ec (:list x things)
(:let foo (foo? x))
(:let bar (bar? x))
(:let baz (baz? x))
(begin (when foo (enqueue! foos x))
(when bar (enqueue! bars x))
(when baz (enqueue! bazes x))
(when (and foo bar baz) (enqueue! all x))))
(apply values (map queue->list (list foos bars bazes all))))


Testing:

(define foo? even?)
(define bar? positive?)
(define baz? (curry < 3))
(define things (range -8 20))

(let ((foos (make-queue)) (bars (make-queue)) (bazes (make-queue))
(all (make-queue)))
(do-ec (:list x things)
(:let foo (foo? x))
(:let bar (bar? x))
(:let baz (baz? x))
(begin (when foo (enqueue! foos x))
(when bar (enqueue! bars x))
(when baz (enqueue! bazes x))
(when (and foo bar baz) (enqueue! all x))))
(apply values (map queue->list (list foos bars bazes all))))

'(-8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18)
'(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)
'(4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)
'(4 6 8 10 12 14 16 18)


--
If confirmed, Garland would be the fourth Jewish justice on the nation's
highest court.... The three current Jewish members of the Supreme Court are
Ruth Bader Ginsburg, Elana Kagan, and Stephen Breyer.
www.jta.org/2016/03/16/news-opinion/united-states/obama-to-name-jewish-federal-judge-to-supreme-court

Robert L.

unread,
Aug 23, 2017, 11:42:39 AM8/23/17
to
Barry Margolin wrote:

> Peter Seibel <pe...@javamonkey.com> wrote:
>
> > Joel Ray Holveck <jo...@piquan.org> writes:
> >
> > >> On this point we should probably agree to disagree. Personally,
> > >> while I initially did find LOOP to be large, complex, & confusing, I
> > >> now find a fairly large subset of LOOP to be perspicuous enough, and
> > >> for a number of common tasks to be considerably more convenient that
> > >> DO/DO*, particularly given features like iteration variable
> > >> destructuring and COLLECT/COUNT/SUM/&c.
> > >
> > > Whenever I tell my (non-Lisp) friends about LOOP, I find it hard to
> > > come up with an on-the-spot example that demonstrates its power. It's
> > > almost always something that can be written just as easily using
> > > Perl's "for" (which can behave as our FOR V IN L, or our FOR V = X
> > > THEN Y WHILE Z, like C's for).
> > >
> > > Do you have a good example of LOOP's power / flexibility that doesn't
> > > need much surrounding context to understand?
> >
> > Here are a few:
> >
> > (loop repeat 100 collect (random 10))


(build-list 22 (lambda _ (random 10)))
===>
'(8 7 1 1 0 5 6 2 3 6 8 6 4 8 9 8 1 8 0 5 9 6)


> >
> > (loop for x across array-of-numbers
> > minimizing x into min
> > maximizing x into max
> > summing x into total
> > counting t into count
> > finally (return (list min max (/ total count))))

Why the
counting t into count
???

We're dealing with a vector, not a list. Obtaining
the length is cheap.


(define numbers #(2 3 4 5 6))

(match (vec-multi-reduce (list min max +) numbers)
[`(,mini ,maxi ,total)
(list mini maxi (/ total (vector-length numbers)))])
===>
'(2 6 4)


Given:

(define (vec-multi-reduce funcs the-vector)
(let loop ((i 1)
(accum (make-list (length funcs) (vector-ref the-vector 0))))
(if (< i (vector-length the-vector))
(let ((x (vector-ref the-vector i)))
(loop (+ 1 i) (map (cut <> <> x) funcs accum)))
accum)))


In fact, this task is so mundane that it can easily be done with
Emacs Lisp.

(require 'cl)
(setq list-of-numbers (number-sequence 2 22))

(let* ((count 0) (total 0) (max (car list-of-numbers)) (min max))
(dolist (x list-of-numbers (list min max (/ total count)))
(incf count)
(incf total x)
(setq max (max x max))
(setq min (min x min))))

==> (2 22 12)



> >
> > (loop for w across widgets count (good-widget-p w))


(require srfi/1) ; count iota

(count odd? (iota 20))
===>
10



--
Among things cited by Burke Davis in _The Long Surrender_ was the fact that
after the Battle of Sharpsburg in Maryland, the Northerners announced that they
would not permit anyone to accord Christian burials to the Southern soldiers of
war---they ordered the bodies to be left out to rot and to decompose.
--- Sam Dickson, "Shattering the Icon of Abraham Lincoln"

Robert L.

unread,
Aug 30, 2017, 9:55:10 PM8/30/17
to
Peter Seibel wrote:

> > Do you have a good example of LOOP's power / flexibility that doesn't
> > need much surrounding context to understand?
>
> Here are a few:
>

>
> (loop for x across array-of-numbers
> minimizing x into min
> maximizing x into max
> summing x into total
> counting t into count
> finally (return (list min max (/ total count))))

Since it's an array (vector) of numbers and not a list, why
does he think that he needs "counting t into count"?


(define num-vec #(2 3 4 5 6))

(let ((mini #f) (maxi #f) (total 0))
(dict-for-each num-vec
(lambda (i x)
(inc! total x)
(min! mini x)
(max! maxi x)))
(list mini maxi (/ total (vector-length num-vec))))
===>
(2 6 4)


Given:

(define-syntax inc!
(syntax-rules ()
[(inc! id) (set! id (+ id 1))]
[(inc! id n) (set! id (+ id n))]))

(define-syntax max!
(syntax-rules ()
[(max! id val)
(set! id (if id (max id val) val))]))

(define-syntax min!
(syntax-rules ()
[(min! id val)
(set! id (if id (min id val) val))]))


--
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands.... [T]he real aim stays the
same: the biggest genocide in human history.... --- Nick Griffin
(https://www.youtube.com/watch?v=K0hD7IffTJs)

Robert L.

unread,
Sep 7, 2017, 12:55:23 PM9/7/17
to
Barry Margolin wrote:

> Peter Seibel <pe...@javamonkey.com> wrote:
>
> > Joel Ray Holveck <jo...@piquan.org> writes:
> >
> > >> On this point we should probably agree to disagree. Personally,
> > >> while I initially did find LOOP to be large, complex, & confusing, I
> > >> now find a fairly large subset of LOOP to be perspicuous enough, and
> > >> for a number of common tasks to be considerably more convenient that
> > >> DO/DO*, particularly given features like iteration variable
> > >> destructuring and COLLECT/COUNT/SUM/&c.
> > >
> > > Whenever I tell my (non-Lisp) friends about LOOP, I find it hard to
> > > come up with an on-the-spot example that demonstrates its power. It's
> > > almost always something that can be written just as easily using
> > > Perl's "for" (which can behave as our FOR V IN L, or our FOR V = X
> > > THEN Y WHILE Z, like C's for).
> > >
> > > Do you have a good example of LOOP's power / flexibility that doesn't
> > > need much surrounding context to understand?
> >

> >
> > (loop for x across array-of-numbers
> > minimizing x into min
> > maximizing x into max
> > summing x into total
> > counting t into count
> > finally (return (list min max (/ total count))))


Why the
counting t into count
???

We're dealing with a vector, not a list. Obtaining
the length is cheap. I guess that Petey the monkey
doesn't know the difference between a list and a vector.


(define numbers #(2 33 4 -5 6))

(with (maxi mini (total 0))
(dict-for-each numbers
(lambda (i x)
(max! x maxi)
(min! x mini)
(inc! total x)))
(list mini maxi (/ total (vector-length numbers))))

===>
(-5 33 8)


Given:

(define-syntax max!
(syntax-rules ()
[(max! val id)
(set! id (if (number? id) (max id val) val))]))

(define-syntax min!
(syntax-rules ()
[(min! val id)
(set! id (if (number? id) (min id val) val))]))

(define-syntax inc!
(syntax-rules ()
[(inc! id) (set! id (+ id 1))]
[(inc! id n) (set! id (+ id n))]))

(define-syntax with-aux
(syntax-rules ()
[(with-aux () bindings stuff ...)
(let bindings stuff ...)]
[(with-aux ((id val) more ...) (b ...) stuff ...)
(with-aux (more ...) (b ... (id val)) stuff ...)]
[(with-aux (id more ...) (b ...) stuff ...)
(with-aux (more ...) (b ... (id '())) stuff ...)]))
(define-syntax with
(syntax-rules ()
[(with specs stuff ...) (with-aux specs () stuff ...)]))


--
My armoured cars ... cut across the British lines of retreat [at Dunkirk]....
But then came a more emphatic order that I was to withdraw.... My tanks were
kept halted there for three days.... [Hitler] then astonished us by speaking
with admiration of the British Empire ... [and saying that] he would even offer
to support Britain with troops if she should be involved in any difficulties
anywhere.... --- The German Generals Talk

Robert L.

unread,
Sep 7, 2017, 1:26:03 PM9/7/17
to
Barry Margolin wrote:

> I think these are very good examples. The power of LOOP isn't in the
> iteration; just about every programming language has reasonable
> iteration mechanisms. The more unique feature is the action verbs like
> COLLECT and SUMMING. Some languages may have a couple of these (like
> Perl's 'grep' and 'map'), but I don't think any other language has
> anything close to the set that LOOP offers (similar capabilitites can be
> found in the Series package for CL).

Wrong.

Inexcusable ignorance.

Look at FOR in Portable Standard Lisp. (Margolin may know nothing of a
real Lisp. He may only know CL (COBOL-Like).)


(MINIMIZE <form> <variable>)

Returns the minimum value of <form>. The use of the optional
argument <variable> is described above.

1 lisp> (for (in v '(1 2 3))
(minimize v))
1



(MAXIMAL <value> <test> <variable>)
(MINIMAL <value> <test> <variable>)

These clauses are generalizations of the clauses maximize and
minimize. Maximal determines the greatest value of <test> over
all of the loop iterations. The corresponding value of <value>
is returned. As a particular case it is possible to return the
value of an iteration variable for which some function attains
a maximum value. The functions used for comparisons are
greaterp and lessp. The use of the optional argument
<variable> is described above.

1 lisp> (for (in v '(2 -2))
(minimal v (- (expt v 2) (* 7 v))))
2

--
In Stockholm ... 20 Muslim men ... began to assault the children, ripping their
swimsuits off and beating the boys when they tried to stop the assault....
[T]he men cornered one of the [11-year-old] girls in a grotto in the bathhouse
and gang raped her. The police refused to press any charges.
http://www.liveleak.com/view?i=807_1369627137

Tim Howe

unread,
Sep 17, 2017, 1:30:12 AM9/17/17
to No_sp...@nowhere_7073.org
On 7 Sep 2017, Robert L. <No_spamming@noWhere_7073.org> wrote:

> Barry Margolin wrote:

[...]

> Why the
> counting t into count
> ???

I may be retreading tired ground here (coming back after a longish
hiatus) but I have to wonder why your posts seem to be missing
References headers? It makes it hard to see what you're talking about
and while your posts seem dense with code -- very possibly concise and
informative -- it's difficult to get benefit from your thoughts.

It should be possible to amend the Subject line while keeping context,
for example as in this message.

--
Tim Howe
http://quadium.net/~vsync/

Love of bustle is not industry; it is only the restlessness of a
hunted mind. And true repose does not consist in condemning all
motion as merely vexation; that kind of repose is slackness and
inertia. No, men should combine these tendencies, and he who
reposes should act and he who acts should take repose.
-- Seneca

Go Away W J

unread,
Sep 19, 2017, 9:02:03 PM9/19/17
to
Tim Howe wrote:
> I may be retreading tired ground here (coming back after a longish
> hiatus) but I have to wonder why your posts seem to be missing
> References headers? It makes it hard to see what you're talking about

Better to just KF him, he won't respond.

The missing References headers are probably deliberate. He has tried
other tactics to screw up threading, including deliberate mutilation
of the "Re: " prefix.

> and while your posts seem dense with code -- very possibly concise and
> informative -- it's difficult to get benefit from your thoughts.

They are dense with *off-topic* code. His recent code is all Scheme,
though he never identifies it as Scheme, which must be confusing for
newbies who encounter this newsgroup. Previously he posted off-topic
Ruby code several hundred times.

See <5917b66a$0$1618$c3e8da3$5496...@news.astraweb.com> and
<c088c4f4-22f1-44a0...@googlegroups.com>.

Tim Howe

unread,
Sep 22, 2017, 8:05:19 PM9/22/17
to
On 20 Sep 2017, Go Away W J <go-aw...@example.com.invalid> wrote:

> They are dense with *off-topic* code. His recent code is all Scheme,
> though he never identifies it as Scheme, which must be confusing for
> newbies who encounter this newsgroup.

Ah. Thought I glimpsed a rant about a superior Lisp, so at a glance I
assumed it was some homegrown thing and he just hated ANSI/CL/us.
0 new messages