let next x = match !x with | Nil -> failwith "next" | Cons (h,t) -> h, t | Exception x -> raise x | Frozen f -> try let h, t as ht = f() in x := Cons (h, t); ht with exn -> x := Exception exn; raise exn;; let hd x = fst (next x);; let tl x = snd (next x);; let rec nth l n = if n < 0 then failwith "nth" else if n = 0 then hd l else nth (tl l) (pred n);;