From: Nicolas C. <war...@fr...> - 2005-08-10 17:24:49
|
> On the subject of new list functions, how about: > > let rec list_map_pairs f = function > | [] | [_] -> invalid_arg "list_map_pairs" > | [x; y] -> [f x y] > | x :: (y :: _ as xs) -> > f x y :: list_map_pairs f xs > > We use this sort of pattern to disaggregate data which has been > collected using an incrementing counter - something like this: > > # let xs = [ 0; 1; 4; 5; 5; 5; 8; 9; 9; 10 ];; > val xs : int list = [0; 1; 4; 5; 5; 5; 8; 9; 9; 10] > # list_map_pairs (fun a b -> b - a) xs;; > - : int list = [1; 3; 1; 0; 0; 3; 1; 0; 1] > > Rich. I think that it's not very the Ocaml spirit of handling pairs, in general you will first transform your "int list" into an "(int * int) list" with error handling and then treat pairs uniformly in a typesafe manner. Nicolas |