Kairit wrote:
> I want to write some nice J code using $: instead of nasty for loops.
> I have a monadic verb (let's call it monverb) which I want to call out
> 500
> times with arguments 0 to 499.
Raul responded:
> I would not approach this problem in this way.
> I would use something like
> monverb"0 i.500
I agree with Raul on this. In J, loops are usually accomplished via array
constructs, not recursion. We normally reserve $: for top-down algorithms
where the verb depends upon its own output and we don't know how many
iterations to apply in advance.
In this case, for example, it appears that monverb 5 is independent of
monverb 4 . So, as Raul pointed out, we use " instead of $: .
In fact, I think it would be a fruitful discussion to compare and contrast
the cases where we use:
* "
* /
* ^:iteration_calculator
* ^:condition^:_
* $:
* for. and while.
I say that because I struggled with the third sentence of this message. I
couldn't quite articulate the type of problem which would cause one to use
$: instead of ^: .
-Dan
PS: If you find a case where it is applicable, here is how you could write
the verb you're asking for:
(i.0 0)"_`(monverb , $:@:<:)@.(_1 ~: *) 500
The only interesting part is i.0 0 , the initial condition. This is valid
for the case you described, but in general it would be
i.0#~1+rank_of_monverb_output .
--
View this message in context:
http://www.nabble.com/loopless-code-tp14894557s24193p14896179.html
Sent from the J Programming mailing list archive at Nabble.com.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm