F#2d数组索引错误

时间:2011-11-25 14:15:21

标签: f#

以下代码提供错误The operator 'expr.[idx]' has been used an object of indeterminate type based on information prior to this program point. Consider adding further type constraints。我想我告诉过它的类型。有什么问题?

let board = Array2D.init 30 30 (fun x y -> 0)

let tickCell x y board : int[,]  =
    match board  with
    |board when board.[x].[y] = 0-> 1
    |board when board.[x].[y] = 1-> 0
    | _ -> -1

let board2 = Array2D.init 30 30 (fun x y -> tickCell x y board)

1 个答案:

答案 0 :(得分:2)

这些小改动应该摆脱错误:

  1. 换行board参数并输入括号
  2. 使用[x,y]

    访问单元格
    let tickCell x y (board : int[,])  =
        match board  with
        |board when board.[x,y] = 0-> 1
        |board when board.[x,y] = 1-> 0
        | _ -> -1