1 def ioloop count code =
2 let
3 def _loop index =
4 if (index>=count)
5 iovoid
6 ((code index) >>> (_loop (index+1)))
7 in _loop 0
2 let
3 def _loop index =
4 if (index>=count)
5 iovoid
6 ((code index) >>> (_loop (index+1)))
7 in _loop 0
這個(gè)函數(shù)輸入count,然后執(zhí)行code。code接受一個(gè)參數(shù)代表目前循環(huán)的次數(shù),循環(huán)次數(shù)從0到count-1之后結(jié)束。最后函數(shù)返回IO void。如果循環(huán)中出現(xiàn)錯(cuò)誤,那么立刻返回錯(cuò)誤。我們可以嘗試寫一段代碼:
1 def main125 = do
2 writeln "現(xiàn)在開始計(jì)算1到10的平方:";
3 ioloop 10 \index->do
4 number = return (index+1);
5 write ((itoa number) + "*" + (itoa number) + "=");
6 square = return (number * number);
7 writeln (itoa square);
8 end;
9 writeln "結(jié)束!";
10 end
2 writeln "現(xiàn)在開始計(jì)算1到10的平方:";
3 ioloop 10 \index->do
4 number = return (index+1);
5 write ((itoa number) + "*" + (itoa number) + "=");
6 square = return (number * number);
7 writeln (itoa square);
8 end;
9 writeln "結(jié)束!";
10 end
執(zhí)行結(jié)果如下:
1 現(xiàn)在開始計(jì)算1到10的平方:
2 1*1=1
3 2*2=4
4 3*3=9
5 4*4=16
6 5*5=25
7 6*6=36
8 7*7=49
9 8*8=64
10 9*9=81
11 10*10=100
12 結(jié)束!
13 main125返回值:(system.success (system.pair <VOID> <USER>))
2 1*1=1
3 2*2=4
4 3*3=9
5 4*4=16
6 5*5=25
7 6*6=36
8 7*7=49
9 8*8=64
10 9*9=81
11 10*10=100
12 結(jié)束!
13 main125返回值:(system.success (system.pair <VOID> <USER>))


