如何将标签链接到F#中的可变变量?

时间:2016-07-10 15:12:22

标签: f# mutable

我想在F#中创建一个标签,它使用一个可变变量来返回一个值。不幸的是,F#将此标签设置为常量值。如果mutable的值发生更改,则标签的值仍然存在。它有点不一致吗?有没有办法让标签(" a")依赖于mutable(" 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

1 个答案:

答案 0 :(得分:5)

根据您自己的评论,您希望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
*)