From: Brian H. <bri...@ql...> - 2003-05-22 19:03:29
|
On Thu, 22 May 2003, Michal Moskal wrote: > > 1) For extList: > > val partial_map : ('a -> 'b option) -> 'a list -> 'b list > > let rec partial_map f = function > | x :: xs -> > begin > match f x with > | Some y -> y :: list_partial_map f xs > | None -> list_partial_map f xs > end > | [] -> [] > > modulo tail-recursion tricks. First cut: let partial_map f src = let rec loop dst lst = match lst with [] -> () | h :: t -> match f h with None -> loop dst t | Some b -> let r = [ b ] in Obj.set_field (Obj.repr dst) 1 (Obj.repr r); loop r t in let rec find_head lst = match lst with [] -> [] | h :: t -> match f h with None -> find_head t | Some b -> let r = [ b ] in loop r t; r in find_head src > > 2) Int module like: > > module Int = > struct > type t = int > let compare x y = x - y > end Compare needs to be a little bit more intelligent to handle carry. Consider that compare min_int max_int = 1, i.e. min_int > max_int. While Pervasive.compare min_int max_int = -1 (which is correct). Optimization question for the list at large: would doing: let Int.compare (x : int) (y : int) : int = Pervasives.compare x y be enough to allow the optimizer to do it's thing? Brian |