From: Nicolas C. <war...@fr...> - 2003-06-17 04:00:04
|
> > Ok, so let's provide them a "list only" version of fold_right ( ExtList.fold > > ? ) that will do the job in a tail-rec way using internals set_cdr to > > construct the list in the user-wanted way. This of course does not cover all > > the cases, but should be enough to preserve speed where it is needed (btw > > enums should be even better for this kind of things ^^;) . The difference of > > this fold is that we are iterating from left_to_right but I don't think that > > a lot of users actually wants to do so. > > Well, we already have List.map to convert a 'a list to a 'b list and keep > the order the same. 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) 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 Nicolas Cannasse |