我在写一个交换两个引用调用内容的函数。OCaml中的可变类型
let swap (x : a ref) (y :'a ref) : unit =
其中
type 'a ref = {mutable contents : 'a}
我不知道该采取以解决这个什么办法。我使用模式匹配吗?
这是我的测试案例
let test() : bool =
let r1 = { contents = 5 } in
let r2 = { contents = 6 } in
let _ = swap r1 r2 in
((6,5) = (r1.contents,r2.contents))
;; run_test "Swap different" test
你可以写'ref 5'来代替'{contents = 5}'。 – sepp2k 2013-02-19 14:59:39