F#从循环返回

最低

我有以下我要在F#中实现的C ++代码。我收到一条带有F#段(if语句之间的行)的错误消息。据我了解,语句“ i”不适用于函数,而是适用于“ 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
古斯

您无法中断F#中的循环,但是不用担心,当您习惯使用F#时,实际上就更容易了:

let move (board:array<string>) :int =
    match Array.tryFindIndex (fun e -> e <> "*") board with
    | Some n -> n
    | None   -> -1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章