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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 1CA86BC58 for ; Tue, 25 Aug 2009 00:26:26 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlcBANqwkkpCbwQZkGdsb2JhbACbCAEBAQEJCQwHEwS7QQWEGg X-IronPort-AV: E=Sophos;i="4.44,267,1249250400"; d="scan'208";a="34858486" Received: from out1.smtp.messagingengine.com ([66.111.4.25]) by mail1-smtp-roc.national.inria.fr with ESMTP; 25 Aug 2009 00:26:19 +0200 Received: from compute1.internal (compute1.internal [10.202.2.41]) by gateway1.messagingengine.com (Postfix) with ESMTP id B686257531; Mon, 24 Aug 2009 18:26:17 -0400 (EDT) Received: from heartbeat1.messagingengine.com ([10.202.2.160]) by compute1.internal (MEProxy); Mon, 24 Aug 2009 18:26:17 -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=t5FxKtZvruKgV3LFmclhM/IAVkk=; b=Y9gpNGbxuv64DUHrgxbysURCR5itN46JN6qc4k4507HBqHNhSsu7aiegHVyXPDKdZFDkghD07dvFSRSrXl2y1a+GjpgdiMxVxb16TTBaU+Cw9p6i4tTTu9fY69IOBWEN3HUDn+bVtjqslwgKTHsNqP9CyJEFk+lmCmZDI/rm38A= X-Sasl-enc: irmAi78QeQNXkJwDGNLCABMybuU0CUcKyPSjdWZJ8uOX 1251152777 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 D1E4058E11; Mon, 24 Aug 2009 18:26:16 -0400 (EDT) Message-ID: <4A9311E4.7060600@ens-lyon.org> Date: Tue, 25 Aug 2009 00:19:16 +0200 From: Martin Jambon User-Agent: Thunderbird 2.0.0.17 (X11/20081008) MIME-Version: 1.0 To: =?UTF-8?B?U3TDqXBoYW5lIEdsb25kdQ==?= Cc: Warren Harris , OCaml Subject: Re: [Caml-list] lazy vs fun References: <94AD5806-B6F6-44F7-AA3C-1E63B6C1A722@metaweb.com> <4A930EF0.3050900@glondu.net> In-Reply-To: <4A930EF0.3050900@glondu.net> 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 gettimeofday:01 val:01 syntax:01 phane:98 warren:98 wrote:01 unix:01 unix:01 caml-list:01 cma:01 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 # #load"unix.cma";; # let first_date = lz Unix.gettimeofday;; val first_date : unit -> float = # first_date ();; - : float = 1251151837.4585979 # first_date ();; - : float = 1251151837.4585979 However this is slightly less efficient than how "lazy" is implemented, and of course you don't have the nice syntax nor the (recent) pattern matching feature. Martin -- http://mjambon.com/