December 17, 2003
Andreas Fuchs has ported ITERATE from the CMU AI repository to ANSI lisp: “In short, the ITERATE macro is an
extensible, more flexible and more lispy replacement for LOOP.”
Andreas' port, is available under the MIT license at http://boinkor.net/lisp/iterate-ansi-0.9.1.tgz.
(iterate (for (key . item) in alist)
(collect key into keys)
(collect item into items)
(finally (return (values keys items))))
(iterate (generate i from 0 to 6)
(for (key . value) in '((a . 2) (zero . 10) (one . 20) (d . 5)))
(when (>= value 10)
(collect (cons key (next i)))))
=> ((ZERO . 0) (ONE . 1))
When other languages, with non-uniform syntaxes, add a feature like this the language developers add new keywords, users are forced to upgrade, and a flurry of articles are published on how to use the new facility. Sad.
Posted by jjwiseman at December 17, 2003 09:48 AM
The result for the second example is not, as Edi Weitz noted on the Cliki page lready, '((B . 0) (C . 1)), but ((ZERO . 0) (ONE . 1)).
I guess that's what you get when you modify examples and don't run them again (-:
Thanks, Andreas, it's fixed now. That's what I get for not even loading the code...
Hi! Maybe off-topic, but here it comes: Is there any "standard" way of finding where a maximum/minimum of a sequence is? I mean, there is a native "max" in Lisp that founds a maximum; and there is "loop...maximize" or "iterate...maximize".
I'm looking for a kinda "argmaximize". I'm at present hacking the "cl" Emacs Lisp package, intending that the construct "loop for Xi being the elements of X using (index i)" admits a clause "argmaximizing i Xi into var" so "var" holds the "i" where "Xi" is maximum.
"Iterate" code seems quite clean, in order to try the same thing.
Bye, and thanks for Lemonodor.
It's not clear to me from your posting if you're aware of the fact that iterate can do that already, so here goes:
* (iter (for x below pi by .0001) (finding x maximizing (sin x)))
And of course you can do pretty much the same thing with loop:
cl-user(1): (loop for x below pi by .0001 maximizing (sin x))
Ops, I was not aware of "iterate"'s "finding" clause. I should have read more carefully its code before posting (and Cliki: http://www.cliki.net/iterate). But I fail to see the "argmax" point in Waiter's "loop" example.
Thank you very much.
My mistake. It was a bit late and I didn't read your question carefully enough. Sorry.
Surely my bad English did help, too.