F# Clearly defined mutable variable 报错it is not a mutable variable
F# Clearly defined mutable variable errors it is not a mutable variable
我的代码中有几个可变变量。除了一个,其他都有效!
变量 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))
在匹配中使用 d
会导致使用该版本的 d
而不是定义为可变值的 d
。
直接将值的名称更改为其他名称1
。
例如:| d -> match (exp.[((List.length exp)-2)])
可以变成| 1 -> match (exp.[((List.length exp)-2)])
我的代码中有几个可变变量。除了一个,其他都有效!
变量 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))
在匹配中使用 d
会导致使用该版本的 d
而不是定义为可变值的 d
。
直接将值的名称更改为其他名称1
。
例如:| d -> match (exp.[((List.length exp)-2)])
可以变成| 1 -> match (exp.[((List.length exp)-2)])