From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 494CCBBAF for ; Tue, 25 Aug 2009 01:18:58 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlcBADq9kkpCbwQZkGdsb2JhbACbCAEBAQEJCQwHEwS7OwWEGg X-IronPort-AV: E=Sophos;i="4.44,267,1249250400"; d="scan'208";a="32848266" Received: from out1.smtp.messagingengine.com ([66.111.4.25]) by mail3-smtp-sop.national.inria.fr with ESMTP; 25 Aug 2009 01:18:57 +0200 Received: from compute1.internal (compute1.internal [10.202.2.41]) by gateway1.messagingengine.com (Postfix) with ESMTP id 589B057849; Mon, 24 Aug 2009 19:18:56 -0400 (EDT) Received: from heartbeat2.messagingengine.com ([10.202.2.161]) by compute1.internal (MEProxy); Mon, 24 Aug 2009 19:18:56 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=messagingengine.com; h=message-id:date:from:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; s=smtpout; bh=l4daZRQjTN/npM9NwawUkGiWnaw=; b=LJFzbkzHSA88ft+sFEHth1T2YT9MHY5sLNrZ+X88HnLq0efhjgEoNuKgGl926sqeJNIePtzXi8lWfF3aEQWllAW0TtDkPJnrh+qjbROdcqfvLfA3Vn2tV0XvqXSC3/9UwYTk9DOvWg6SB5qv1BOJP6q3pILaDy6U7gfcdEt6qWc= X-Sasl-enc: eWOKO/pdXitEYoVlZIqmUC80YUOAn6Z2a2uaTs1d1KuA 1251155935 Received: from [192.168.1.12] (ALyon-157-1-44-52.w83-201.abo.wanadoo.fr [83.201.75.52]) by mail.messagingengine.com (Postfix) with ESMTPSA id 5BC3D14A81; Mon, 24 Aug 2009 19:18:55 -0400 (EDT) Message-ID: <4A931E3B.4010501@ens-lyon.org> Date: Tue, 25 Aug 2009 01:11:55 +0200 From: Martin Jambon User-Agent: Thunderbird 2.0.0.17 (X11/20081008) MIME-Version: 1.0 To: Martin Jambon Cc: =?UTF-8?B?U3TDqXBoYW5lIEdsb25kdQ==?= , Warren Harris , OCaml Subject: Re: [Caml-list] lazy vs fun References: <94AD5806-B6F6-44F7-AA3C-1E63B6C1A722@metaweb.com> <4A930EF0.3050900@glondu.net> <4A9311E4.7060600@ens-lyon.org> In-Reply-To: <4A9311E4.7060600@ens-lyon.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; ens-lyon:01 ocaml:01 memoization:01 exn:01 exn:01 phane:98 warren:98 wrote:01 wrote:01 caml-list:01 oops:01 jambon:01 jambon:01 lazy:02 lazy:02 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/