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"