From: Brian H. <bri...@ql...> - 2003-06-17 17:58:12
|
On Tue, 17 Jun 2003, Nicolas Cannasse wrote: > List.map is not the same as List.fold ! > - with fold, you can "skip" elements in a list, doing then a filter_map > - you can access the accumulator , and this is useful in many cases (no > duplicates for example) Point. > > I suggest having something like : > > let fold f l = > let x = [ Obj.magic () ] in > let rec loop dst = function > | [] -> () > | h :: t -> > let r = [ f h (List.tl x) ] in > Obj.set_field (Obj.repr dst) 1 (Obj.repr r); > loop r t > in > loop x; > List.tl x Maybe combine this with filter_map-like capabilities? Something like: let foo f l = let x = [ Obj.magic () ] in let rec loop dst = function | [] -> () | h :: t -> match f h (List.tl x) with None -> loop dst t | Some x -> let r = [ x ] in Obj.set_field (Obj.repr dst) 1 (Obj.repr r); loop r t in loop x lst; List.tl x Brian |