我正在尝试在F#中实现以下C ++代码。我收到一条带有F#段的错误消息(if语句之间的行)。据我了解,声明"我"不适用于该功能,而适用于" for"循环?
C ++代码
int move( Board b )
{
for( int i = 0; i < b.size(); ++i )
if( b(i) != "*" )
return i;
return -1;
}
F#代码
let move (board:array<string>) :int =
for i = 0 to (board.Length-1) do
if( Array.item(i) board <> "*" ) then
i
done
-1
答案 0 :(得分:6)
你不能在F#中打破一个循环,但不要担心,当你习惯了F#时,你不会需要,它实际上更容易:<\ n \ n <\ n \ n
/ p>let move (board:array<string>) :int =
match Array.tryFindIndex (fun e -> e <> "*") board with
| Some n -> n
| None -> -1