Martin Jambon wrote: > Stéphane Glondu wrote: >> Warren Harris a écrit : >>> Is there any advantage to using lazy evaluation in ocaml rather than >>> just using thunks to defer evaluation? [...] >> Two things I can think of right now: they are evaluated only once (even >> if you call Lazy.force several times), and you can do pattern matching >> with them. > > > Note that the memoization feature can be implemented like this: > > let lz f = > let result = ref `None in > fun () -> > match !result with > `None -> > (try > let y = f () in > result := `Result y; > y > with e -> > result := `Exn e; > raise e > ) > | `Result y -> y > | `Exn e -> raise e
Oops. The following makes it possible for f to be garbage-collected: let lz f = let result = ref (`None f) in fun () -> match !result with `None f -> (try let y = f () in result := `Result y; y with e -> result := `Exn e; raise e ) | `Result y -> y | `Exn e -> raise e Martin -- http://mjambon.com/ _______________________________________________ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs