4

i have seen in ruby as well powershell programming we can assign variables like a,b=b,a . it actually swaps the variable .

Is this possible in f# if so please guide me with some reference

4

1 回答 1

13

通常,F# 不允许变量重新分配。相反,它通过 let 绑定支持不可变的命名值。因此,以下是不可能的:

let a = 3
a = 4

除非您明确标记amutable

let mutable a = 3
a <- 4

然而,在大多数情况下,F# 确实允许变量“遮蔽”。对此的唯一限制是它不能在顶级模块上完成。但是,在一个函数中,例如,以下工作正常:

let f () =
    let a,b = 1,2
    let a,b = b,a //"swap"
    a,b
于 2011-04-04T03:53:47.900 回答