我如何 link F# 中可变变量的标签?

How can I link a label to a mutable variable in F#?

我想在 F# 中创建一个标签,它使用一个可变变量来 return 一个值。不幸的是,F# 将此标签设置为常量值。如果可变的值发生变化,标签的值将保持不变。是不是有点不协调?有没有办法让标签("a")依赖于可变的("x")?

let mutable x = 0;

let a = x + 2; // I want not to set a to a constant value
let b two = x + two;

x <- 1;

let c = b 2;

let isConsistent = a = c;


val mutable x : int = 1
val a : int = 2
val b : two:int -> int
val c : int = 3
val isConsistent : bool = false

根据您自己的评论,您希望 a 成为一个 返回 x + 2
的函数 直接翻译为:

let mutable x = 0

let a () = x + 2
let b two = x + two

x <- 1

let c = b 2

let isConsistent = a () = c // don't forget to call the function 'a'

(*
val mutable x : int = 1
val a : unit -> int
val b : two:int -> int
val c : int = 3
val isConsistent : bool = true
*)