我遇到了这个双端队列的实现:
type 'a elem = { mutable l1 : 'a elem; mutable l2 : 'a elem; v : 'a option }
type 'a queue = { mutable front : 'a elem; mutable back : 'a elem }
let init () =
let rec g1 = { l1 = g1; l2 = g2; v = None}
and g2 = { l1 = g2; l2 = g1; v = None}
in
{ front = g1; back = g2 }
let is_empty q =
let f = q.front
and b = q.back
in
f.l2 == b
let put_between p q x =
let r = { l1 = p; l2 = q; v = Some x }
in begin
if p.l1 == q then p.l1 <- r else p.l2 <- r;
if q.l1 == p then q.l1 <- r else q.l2 <- r
end
我真的不明白这个实现的主要思想是什么,主要概念是什么?你能给我解释一下吗?为什么我们使用这些记录相互递归?