> > You can do better with an ad-hoc encoding of the continuation > instead of using closures: > > let rec map k f = function > | [] -> List.rev k > | x :: rl -> map (f x :: k) f rl > ;; > > The memory footprint is smaller, and you spend much less time > invoking closures. > > Note that I haven't bothered benchmarking these two functions. I did the benchmark with four version of map (below): List of size 10, 10000000 times with standard map : 0.948059s List of size 10, 10000000 times with map with rev : 1.800112s List of size 10, 10000000 times with map with prelist : 3.060192s List of size 10, 10000000 times with map with obj : 1.704105s List of size 100, 1000000 times with standard map : 1.068068s List of size 100, 1000000 times with map with rev : 1.448090s List of size 100, 1000000 times with map with prelist : 2.668166s List of size 100, 1000000 times with map with obj : 1.652104s List of size 1000, 100000 times with standard map : 1.792112s List of size 1000, 100000 times with map with rev : 2.912182s List of size 1000, 100000 times with map with prelist : 3.520220s List of size 1000, 100000 times with map with obj : 2.460154s List of size 10000, 10000 times with standard map : 7.564473s List of size 10000, 10000 times with map with rev : 15.452965s List of size 10000, 10000 times with map with prelist : 12.672792s List of size 10000, 10000 times with map with obj : 11.572724s List of size 100000, 1000 times with standard map : 33.018063s List of size 100000, 1000 times with map with rev : 42.142634s List of size 100000, 1000 times with map with prelist : 22.161385s List of size 100000, 1000 times with map with obj : 20.801299s standard map with size 1000000 segfaults on my machine List of size 1000000, 100 times with map with rev : 55.211450s List of size 1000000, 100 times with map with prelist : 23.549472s List of size 1000000, 100 times with map with obj : 21.777361s standard map = List.map map with rev = the above code given by Damien Doligez map with prelist = a dirty map using Obj but through a not too dirty, but not completely safe interface prelist (attached) : let pmap f l = let pl = start () in let rec fn = function [] -> Prelist.extract pl | a::l -> Prelist.cons (f a) pl; fn l in fn l map with obj : a directly dirty obj map : let objmap f l = match l with [] -> [] | x::l' -> let start = [f x] in let rec fn current = function [] -> start | x::l' -> let l'' = [f x] in Obj.set_field (Obj.repr current) 1 (Obj.repr l''); fn l'' l' in fn start l' Conclusion : dirty map wins for large lists, Standard map wins for small lists, but if you map a non trivial function (here I map the trivial succ function on int), there should be no difference so I would suggest to use map with reverse. The conclusion is that I would replace Xavier's suggestion in another thread : << Repeat after me: "Obj.magic is not part of the OCaml language". >> By << Try very very very hard not to use object. If it fails try very hard to use Obj but no C. >> I still think Obj is safer than C (I do not speak for interfacing with external library where C is mandatory), but you should be aware that Obj needs as much knowledge about the runtime than C interface without using the provided C macro ... Cheers, Christophe -- Christophe Raffalli Universite de Savoie Batiment Le Chablais, bureau 21 73376 Le Bourget-du-Lac Cedex tel: (33) 4 79 75 81 03 fax: (33) 4 79 75 87 42 mail: Christophe.Raffalli@univ-savoie.fr www: http://www.lama.univ-savoie.fr/~RAFFALLI --------------------------------------------- IMPORTANT: this mail is signed using PGP/MIME At least Enigmail/Mozilla, mutt or evolution can check this signature. The public key is stored on www.keyserver.net ---------------------------------------------