Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 40 |
Nodes: | 6 (0 / 6) |
Uptime: | 10:22:06 |
Calls: | 291 |
Files: | 910 |
Messages: | 76,423 |
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))
(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))))
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))
newLISP
(collect (rand 10) 100)
(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))))
(define array-of-numbers (array 9 '(2 -2 0 9 7 8 3 4 3)))
(local (mx mn total cnt)
(dolist (n array-of-numbers)
(setq mx (max (or mx n) n))
(setq mn (min (or mn n) n))
(++ total n)
(++ cnt))
(list mn mx (div total cnt)))
(-2 9 3.777777777777778)
Another way:
(list (apply min array-of-numbers)
(apply max array-of-numbers)
(div (apply + array-of-numbers) (length array-of-numbers)))
Or if the predicate functions FOO-P and BAR-P are sufficiently
expensive that you don't want to compute more often than absolutely necessary:
(loop for x in things
for foo-p = (foo-p x)
for bar-p = (bar-p x)
when foo-p collect x into foos
when bar-p collect x into bars
when (and foo-p bar-p) collect x into both
finally (return (values foos bars both)))
(loop for n below 3 do (print n))
That ain't Lisp syntax. This is Lisp syntax;
(do ((n 1 (1+ n)))
((> n 3))
(print n))