F#明确定义了可变变量错误,它不是一个可变变量

时间:2017-01-10 14:05:58

标签: variables f# mutable

我的代码中有几个可变变量。除了一个以外,所有这些都有效! 变量d会出现几个错误,例如

learn.fsx(33,25): error FS0027: This value is not mutable. Consider using the mutable keyword, e.g. 'let mutable d = expression'

问题是当你查看我的代码时,变量显然被定义为一个可变变量。

我认为这是必要的,因为我能想到的唯一能引起问题的是它是变量定义之后的东西,它使它再次变得不可变。

let seq = [2;2;3;3;5;6]
let exp = [[];[]]
let mutable points = 0
let mutable e = 1
let mutable state = ""
let mutable d = 1
let rec guess (x:int) =
   match points with
   |100 -> "learned"
   |_ -> match seq.[x] with
         |d -> match (exp.[((List.length exp)-2)]) with
               |[] -> if state = "right" then
                        exp.[((List.length exp)-1)]@[d]
                      else
                        state <- "right"
                        exp@[[d]]
                      points <- points + 1
                      if d = 6 then
                         d <- 1
                      else
                         d <- d + 1
                      if x = 5 then
                        (guess 0)
                      else
                        (guess (x+1))
               |_ ->  if state = "right" then
                        exp.[((List.length exp)-1)]@[d]
                      else
                        state <- "right"
                        exp@[[d]]
                      if (List.length exp.[((List.length exp)-2)]) >= 2 then
                        d <- (exp.[((List.length exp)-2)]).[e]
                      else
                        if d = 6 then
                           d <- 1
                        else
                           d <- d + 1
                      e <- e + 1
                      if x = 5 then
                        (guess 0)
                      else
                        (guess (x+1))
         |_ -> points <- points - 1
               e <- 1
               state <- "wrong"
               if d = 6 then
                  d <- 1
               else
                  d <- d + 1
               if x = 5 then
                 (guess 0)
               else
                 (guess (x+1))

1 个答案:

答案 0 :(得分:5)

在匹配项中使用d会导致使用d版本,而不是定义为可变值的d

直接将值的名称更改为其他使用1

例如:| d -> match (exp.[((List.length exp)-2)])可以成为| 1 -> match (exp.[((List.length exp)-2)])

相关问题