我需要在OCaml中测试一个5元素数组的所有可能组合,如果任何组合符合条件,我必须停止循环并返回该数组,但在for循环中返回结果并不容易...
我有以下代码:
let myfunction t =
let arraycolours = Array.make 5 W in
try
for i=0 to 3 do
Array.set arraycolours 0 (inttocolour i);
for j=0 to 3 do
Array.set arraycolours 1 (inttocolour j);
for k=0 to 3 do
Array.set arraycolours 2 (inttocolour k);
for l=0 to 3 do
Array.set arraycolours 3 (inttocolour l);
for m=0 to 3 do
Array.set arraycolours 4 (inttocolour m);
if test arraycolours = t then raise Exit
done
done
done
done
done
with Exit -> arraycolours;;
但是报错说: 错误:此表达式的类型为颜色数组,但需要一个类型为单元的表达式。
我该如何返回符合条件的数组?