for. T do. B end. for_xyz. T do. B end.The B block is evaluated once for each item of the array A that results from evaluating the T block. In the for_xyz. form, the local name xyz is set to the value of an item on each evaluation of the B block and xyz_index is set to the index of the item; results are not dependable if these names are re-assigned within the B block. break. goes to the end of the for. control structure, and continue. goes to the evaluation of B for the next item. |
f0=: 3 : 0
s=. 0
for. i. y do. s=.>:s end.
)
(f0 = ])"0 ?5$100
1 1 1 1 1
f1=: 3 : 0
s=.0
for_j. i.y do.
if. 2|j do. continue. end.
s=.j+s
end.
)
(f1 = 2&!@>.&.-:)"0 ?5$100
1 1 1 1 1
comb=: 4 : 0 NB. All size x combinations of i.y
k=. i.>:d=.y-x
z=. (d$<i.0 0),<i.1 0
for. i.x do. z=. k ,.&.> ,&.>/\. >:&.> z end.
; z
)
3 comb 5
0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 2 3
1 2 4
1 3 4
2 3 4
queens=: 3 : 0 NB. solves the n queens problem
z=.i.n,*n=.y
for. }.z do.
b=. -. (i.n) e."1 ,. z +"1 _ ((-i.){:$z) */ _1 0 1
z=. ((+/"1 b)#z),.n|I.,b
end.
)
queens 5
0 2 4 1 3
0 3 1 4 2
1 3 0 2 4
1 4 2 0 3
2 0 3 1 4
2 4 1 3 0
3 0 2 4 1
3 1 4 2 0
4 1 3 0 2
4 2 0 3 1