如何返回多个值并将它们分配给可变变量?

时间:2022-08-23 10:14:03

This is what I have so far.

这就是我到目前为止所拥有的。

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)

4 个解决方案

#1


You can't; there's no syntax to update 'more than one mutable variable' with a single assignment. Of course you can do

你不能;没有语法可以使用单个赋值更新“多个可变变量”。当然可以

let newX, newY = Swap(x,y)
x <- newX
y <- newY

#2


The code you have commented doesn't work because when you write "x, y" you create a new tuple that is an immutable value, so can't be updated. You could create a mutable tuple and then overwrite it with the result of the swap function if you want:

您评论的代码不起作用,因为当您编写“x,y”时,您创建一个新的元组,这是一个不可变的值,因此无法更新。您可以创建一个可变元组,然后使用swap函数的结果覆盖它,如果您需要:

let mutable toto = 5, 10 

let swap (x, y) = y, x

toto  <- swap toto

My advice would be to investigate the immutable side of F#, look at the ways you can use immutable structures to achieve what you previously would have done using mutable values.

我的建议是调查F#的不可变方面,看看你可以使用不可变结构来实现你以前使用可变值所做的事情的方法。

Rob

#3


F# has "by reference" parameters just like C#, so you can write a classic swap function similarly:

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

#4


To expand on Robert's answer:

扩展罗伯特的答案:

let swap (x : int, y : int) = y, x
let mutable x = 5
let mutable y = 10
let mutable xy = x, y

xy <- swap xy

Makes both the variables and the tuple mutable.

使变量和元组都可变。

#1


You can't; there's no syntax to update 'more than one mutable variable' with a single assignment. Of course you can do

你不能;没有语法可以使用单个赋值更新“多个可变变量”。当然可以

let newX, newY = Swap(x,y)
x <- newX
y <- newY

#2


The code you have commented doesn't work because when you write "x, y" you create a new tuple that is an immutable value, so can't be updated. You could create a mutable tuple and then overwrite it with the result of the swap function if you want:

您评论的代码不起作用,因为当您编写“x,y”时,您创建一个新的元组,这是一个不可变的值,因此无法更新。您可以创建一个可变元组,然后使用swap函数的结果覆盖它,如果您需要:

let mutable toto = 5, 10 

let swap (x, y) = y, x

toto  <- swap toto

My advice would be to investigate the immutable side of F#, look at the ways you can use immutable structures to achieve what you previously would have done using mutable values.

我的建议是调查F#的不可变方面,看看你可以使用不可变结构来实现你以前使用可变值所做的事情的方法。

Rob

#3


F# has "by reference" parameters just like C#, so you can write a classic swap function similarly:

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

#4


To expand on Robert's answer:

扩展罗伯特的答案:

let swap (x : int, y : int) = y, x
let mutable x = 5
let mutable y = 10
let mutable xy = x, y

xy <- swap xy

Makes both the variables and the tuple mutable.

使变量和元组都可变。