Вопрос

Could someone kindly explain the control structure in J (specifically For and While loops)? Let's say I have a=:1 and b=:10, and I want to add '1' to 'a' using For/While loops till a < b.

So, usually it would be something like (in other languages)

for i= 1 to b   
a=a+1    
next i

or

while a<=b    
a=a+1    
end

Help please. I know it's simple, but I can't figure out how to do this in loops.

Also, I read up on something called "explicits". For example:

foo=: 3 : 0
if. 1 do. wdinfo 'success' end.
)

What would the 3 and 0 denote?

Это было полезно?

Решение

Control structures must be used within an explicit definition. The explicit definition link will explain what the "3" and "0" signify, but note that the following are equivalent.

myfunction=: 3 : 0
  NB. define function/verb here
) 

myfunction=: verb define
  NB. define function/verb here
) 

Here is an example of defining an explicit function/verb containing a for loop:

for_eg=: verb define
 a=. 3
 b=. 8
 for. i.b do.
   a=. a + 1
 end. 
 a
)

Here is an example of using that verb:

for_eg ''
11

The Jwiki contains a wiki page describing how to define a verb. Note the references at the bottom for more info.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top