这就是我到目前为止所拥有的.
let Swap (left : int , right : int ) = (right, left)
let mutable x = 5
let mutable y = 10
let (newX, newY) = Swap(x, y) //<--this works
//none of these seem to work
//x, y <- Swap(x, y)
//(x, y) <- Swap(x, y)
//(x, y) <- Swap(x, y)
//do (x, y) = Swap(x, y)
//let (x, y) = Swap(x, y)
//do (x, y) <- Swap(x, y)
//let (x, y) <- Swap(x, y)
Run Code Online (Sandbox Code Playgroud)
Bri*_*ian 11
你不能; 没有语法可以使用单个赋值更新"多个可变变量".当然可以
let newX, newY = Swap(x,y)
x <- newX
y <- newY
Run Code Online (Sandbox Code Playgroud)
F# 与 C# 一样具有“通过引用”参数,因此您可以类似地编写经典的交换函数:
let swap (x: byref<'a>) (y: byref<'a>) =
let temp = x
x <- y
y <- temp
let mutable x,y = 1,2
swap &x &y
Run Code Online (Sandbox Code Playgroud)