Browse thread
The Implicit Accumulator: a design pattern using optional arguments
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Bill Wood <william.wood3@c...> |
| Subject: | Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments |
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'.