From: Christophe P. <chr...@gm...> - 2005-12-06 10:24:09
|
Hello, I have here a function I would like to propose for the list module. It's a foldmap. Basically it's a map but it allows you to pass along a variable among the different calls. Perhaps it could be called foldmap_left such that a foldmap_right could be implemented as well. let foldmap f b l =3D let (e, l') =3D List.fold_left (fun (x, t) a -> let (x', a') =3D f x a in (x', a'::t)) (b, []) l in (e, List.rev l') An example of it's use: foldmap (fun a e -> (a+1, e)) 0 [1;2;3] =3D> (3, [1;2;3]) The motivation for this is that sometimes you want to map a certain list (imagine for instance a list of statements in a compiler), but you also wan= t to gradually build up a functional environment as you pass down this list. Cheers, Christophe |