# F＃：编写一个函数，以递归方式构建元组列表并更改可变变量F# : Writing a function that builds a list of tuples recursively and change a mutable variable

``````let GetSameColorNeighs (grid:Option<Ball>[,], row, col, color:Color) =
let rec loop (row, col) = seq {
if not (row < 0 || col < 0 || row > MaxLineNumber - 1
|| col > BallsPerLine - 1) then
let ball = grid.[row,col]
match ball with
| Some(ball) ->
if (!ball.visited = false || not <| ball.color.Equals(color)) then
// Not sure what you want here - yield items using 'yield'?
// [row , col]
else
ball.visited := true
yield row, col                 // Add single item to results
yield! loop(row + 1, col + 1)  // Add all generated to results
yield! loop(row - 1, col - 1)  //        -- || --
| None  -> () }
loop(row, col) |> Seq.toList
``````

``````let GetSameColorNeighs (grid:Option<Ball>[,], row, col, color:Color)  : List<int * int> * bool =
let mutable b : bool = false
let rec loop (row, col) = seq {
if not (row < 0 || col < 0 || row > MaxLineNumber - 1
|| col > BallsPerLine - 1) then
let ball = grid.[row,col]
match ball with
| Some(ball) ->
if (ball.visited = true || not <| ball.color.Equals(color)) then
()
else
//HERE's THE PROBLEM
if (ball_satisfy_a_certain_condition) then
b <- true
ball.visited := true
yield row, col                 // Add single item to results
yield! loop(row + 1, col + 1)  // Add all generated to results
yield! loop(row - 1, col - 1)  //        -- || --
| None  -> () }
loop(row, col) |> Seq.toList, b
``````

1. 为什么上面赋值给一个可变变量错了？
2. 我应该如何重构我的代码以实现这一目标？

## 未解决问题？本站智能推荐：

2回复

F＃中有些模拟？ 就像是 ？
1回复

2回复

2回复

1回复

2回复

1回复

3回复

1回复

3回复

### F＃可变为不可变

Gday All， 我一直在涉及一些F＃，我想出了一些我从一些C＃代码中移植的字符串构建器。 它将对象转换为字符串，前提是它传递了属性中定义的正则表达式。 它可能对手头的任务有些过分，但出于学习目的。 目前，BuildString成员使用可变字符串变量updatedTemplat