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=1.4 required=5.0 tests=DNS_FROM_RFC_POST 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 35BCFBC6B for ; Fri, 29 Jun 2007 15:16:43 +0200 (CEST) Received: from rwcrmhc14.comcast.net (rwcrmhc14.comcast.net [204.127.192.84]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5TDGfMW019631 for ; Fri, 29 Jun 2007 15:16:42 +0200 Received: from 192.168.1.100 (c-67-190-229-4.hsd1.mn.comcast.net[67.190.229.4]) by comcast.net (rwcrmhc14) with SMTP id <20070629131640m1400ni133e>; Fri, 29 Jun 2007 13:16:40 +0000 Subject: Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments From: Bill Wood To: Thomas Fischbacher Cc: caml-list@yquem.inria.fr In-Reply-To: <46826C69.5060802@functionality.de> References: <200706271314.35134.jon@ffconsultancy.com> <1A1D6F56-B3DB-4552-969C-9859482175AC@lrde.epita.fr> <46826C69.5060802@functionality.de> Content-Type: text/plain Date: Fri, 29 Jun 2007 08:15:36 -0500 Message-Id: <1183122936.15452.5.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.0.1-1mdk Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 4685063A.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; 0100,:01 ocaml:01 factorial:01 buffer:01 ocaml:01 1.0:98 1.0:98 2.0:98 wrote:01 rec:01 caml-list:01 functions:01 functions:01 lisp:01 int:01 On Wed, 2007-06-27 at 14:55 +0100, Thomas Fischbacher wrote: . . . > One thing OCaml can do better than, say, CMU CL, is to define globally > visible functions that depend on some otherwise inaccessible context, > as in the following example: > > 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) > ;; > > A corresponding > > (let ((buffer (make-array ...))) > (defun float-factorial (n) > ...)) > > just plainly does not work with CMU CL/SBCL. :-( Just to set the record straight on this, Thomas and I did a little experimentation and discovered that change happens -- CMU CL (at least) now does the Right Thing here. You can indeed define stateful functions, like the OCaml one above, in CMU Common Lisp using 'defun'.