Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (32 sloc) 615 Bytes
1;;
#part 1
let v = lazy (print_string "performing lazy computation\n"; sqrt 16.);;
Lazy.force v;;
Lazy.force v;;
#part 2
type 'a lazy_state =
| Delayed of (unit -> 'a)
| Value of 'a
| Exn of exn
;;
#part 3
let create_lazy f = ref (Delayed f);;
let v = create_lazy
(fun () -> print_string "performing lazy computation\n"; sqrt 16.);;
#part 4
let force v =
match !v with
| Value x -> x
| Exn e -> raise e
| Delayed f ->
try
let x = f () in
v := Value x;
x
with exn ->
v := Exn exn;
raise exn
;;
#part 5
force v;;
force v;;