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=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id D1F1FBC6B for ; Wed, 27 Jun 2007 17:24:43 +0200 (CEST) Received: from pih-relay05.plus.net (pih-relay05.plus.net [212.159.14.132]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5RFOhf1022941 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Wed, 27 Jun 2007 17:24:43 +0200 Received: from [80.229.56.224] (helo=beast.local) by pih-relay05.plus.net with esmtp (Exim) id 1I3ZNp-00012Q-Lb for caml-list@yquem.inria.fr; Wed, 27 Jun 2007 16:24:41 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments Date: Wed, 27 Jun 2007 16:18:53 +0100 User-Agent: KMail/1.9.7 References: <200706271314.35134.jon@ffconsultancy.com> <1A1D6F56-B3DB-4552-969C-9859482175AC@lrde.epita.fr> <46826C69.5060802@functionality.de> In-Reply-To: <46826C69.5060802@functionality.de> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200706271618.54156.jon@ffconsultancy.com> X-Miltered: at discorde with ID 4682813B.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 stack:01 factorial:01 factorial:01 ocaml:01 1.0:98 1.0:98 2.0:98 frog:98 heap:01 wrote:01 rec:01 dynamically:01 caml-list:01 lisp:01 On Wednesday 27 June 2007 14:55:53 Thomas Fischbacher wrote: > For > example, if this were LISP, one could often use dynamically scoped (in > the sense of (declare (dynamic-extent buffer-stack))) contextual > variables for great benefit (and these gory details often can also be > hidden quite conveniently under a few (maybe even in-place macrolet) > macros...), but unfortunately, OCaml does not support anything like > that. This seems to be something that Lisp uses to allocate data structures on the stack rather than the heap. Why would you want that? > let float_factorial = > let _known_factorials = ref [|1.0;1.0;2.0;6.0;24.0;120.0;720.0|] in > (fun n -> > let known_factorials = !_known_factorials in > let nr_known = Array.length known_factorials in > if n < nr_known > then > known_factorials.(n) > else > let new_known_factorials = Array.make (n+1) 0.0 in > begin > for i=0 to nr_known-1 do > new_known_factorials.(i) <- known_factorials.(i) > done; > (let rec fill f_pos pos = > if pos > n then () > else > let () = new_known_factorials.(pos) <- f_pos in > fill (f_pos *. (float_of_int (pos+1))) (pos+1) > in > fill (known_factorials.(nr_known-1)*.(float_of_int nr_known)) nr_known); > _known_factorials := new_known_factorials; > new_known_factorials.(n) > end) I can't quite follow that. Is it doing something cleverer than this: let float_factorial = let m = ref [||] in fun n -> try (!m).(n) with _ -> let m' = Array.make (n + 1) 1. in for i=1 to n do m'.(i) <- float i *. m'.(i - 1) done; m := m'; m'.(n);; > Other advanced optimization techniques that can be used for benefit > in very specialized situations include (explicit) continuation coding: > rather than returning a value (e.g. a tuple), you take as an argument > a function to which you then pass on your return value(s). This is quite > useful whenever "execution flow branches out into multiple paths that > have to be taken", Are you referring to CPS? > and may sometimes (though rarely) also be used for > good as a poor man's VALUES/MULTIPLE-VALUE-BIND substitute. Weren't values and multiple-value-bind completely superceded by pattern matching? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. The OCaml Journal http://www.ffconsultancy.com/products/ocaml_journal/?e