WJ wrote:
> Kent M Pitman wrote:
> > Looking at such an example, one might ask why WHEN is there at
> > all. But the answer turns out to be that it's very hard to
> > write a macro in CL that has a "detached" piece of syntax. So
> > you need to have a way to get to the collect point, and it's
> > often in a conditional. Consider:
> > (loop for x in y
> > when (f x)
> > collect (g x))
> > This one you can't write as
> > (dolist (x y)
> > (when (f x)
> > (collect (g x))))
> Clojure:
> user=> (keep #(if (odd? %) (* % %)) [1 2 3 4])
> (1 9)
> > The ability in loop to do even complex things like:
> > (loop for x in '(1 2 3 4 5 6 7)
> > when (evenp x)
> > collect x into evens
> > else
> > collect x into odds
> > finally
> > (return (values evens odds)))
> > => (2 4 6), (1 3 5 7)
> > is considerably easier and clearer than some other alternatives.
> That doesn't look very Lispy to me, Kent.
> Clojure:
> user=> (replace (group-by even? (range 1 8)) [true false])
> [[2 4 6] [1 3 5 7]]
> > Also, being able to vary the collection is handy. Consider:
> > (loop for x in '(a b (c d) e f)
> > when (atom x)
> > collect x
> > else
> > append x)
> > => (A B C D E F)
> That doesn't look very Lispy to me, Kent.
> Clojure:
> user=> (flatten '(a b (c d) e f))
> (a b c d e f)
Kent M. Pitman wrote:
> It's unfortunate that the ability to define other loop-paths was
> omitted from the standard, since most implementations have such
> a thing. Some things I think you have to just code longhand.
> (loop with best-foo = nil ;untested
> for foo in (list foo1 foo2)
> when (or (not best-foo)
> (> (weight foo) (weight best-foo)))
> do (setq best-foo foo)
> finally (return best-foo))
> All in all, I think this is still pretty readable, though I'd accept
> the classic
> (let ((best-foo nil)) ;untested
> (dolist (foo (list foo1 foo2))
> (when (or (not best-foo)
> (> (weight foo) (weight best-foo)))
> (setq best-foo foo)))
> best-foo)
That doesn't look very Lispy to me, Kent.