On 3/5/07, Don Guinn <[EMAIL PROTECTED]> wrote:
I gather you are looking for a more general solution than your specific
example. Look at the conjunction "^:" (Power). Raul's solution is perfect
for your example as it turns the question around in such a way that you
can determine your ending condition without going through all the
calculations. "^:" lets you test your previous result  to determine if you
want to continue. This would still be a loop but certainly not in the
traditional form.

A problem with loops is the "If all you have is a hammer,
everything looks like a nail" issue.

They also tend to act as "kitchen sinks" -- accumulating all
sorts of clutter because people want to cram everything they can
into them.

Even when you have loops, code often factors better if you split
them into multiple loop instances.  Sometimes this hurts
performance, sometimes it helps performance, and often
performance is irrelevant.  Performance matters at bottlenecks,
and while it's fine to turn everything on its ear to fit it
better through a bottleneck this can be bad practice in most
other areas.

That said, if you want to structure your system timewise, and
you've got an appropriate function f which you want to repeat
until its result satisfies some condition b, then f^:b^:_
behaves like a traditional loop.  (Note that this won't
necessarily work if hidden state is involved -- the result of f
has to be different every time or the "loop" will terminate
prematurely.)

But indefinite loops, and/or lazy infinities, are often not the
best approach for non-scalar operations, even if most
programmers are trained to believe otherwise.

--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to