【发布时间】:2013-02-19 22:50:40
【问题描述】:
我正在编写一个交换两个引用调用内容的函数。
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 }。
标签: ocaml