Commits

Anonymous committed 6ec1358 Merge

merge

Comments (0)

Files changed (3)

 - Added Base.find_by_iter and Base.find_in_tree
 * Type change from exn to `Exn of exn at Exn.catch and the other exn result error types 
 - Added Option.to_result
+- Added List.zip, Haskell style combine
 - Added Levenshtein for Levenshtein distance for arrays
 - String.Levenshtein.distance is rewritten using an imperative algorithm.
 
 let accum xsref x = xsref := x :: !xsref
 let (+::=) = accum
 
+let zip xs ys = 
+  let rec zip st xs ys =
+    match xs, ys with
+    | [], _ | _ ,[] -> List.rev st
+    | x::xs, y::ys -> zip ((x,y)::st) xs ys
+  in
+  zip [] xs ys
+
+TEST "zip" =
+      zip [] [] = []
+      && zip [] [1] = []
+      && zip [1] [] = []
+      && zip [1;2;3] [4;5;6;7] = [(1,4); (2,5); (3,6)]
+
 module Infix = struct
   let (--) = (--)
   let (+::=) = (+::=)
 val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
 val partition_map : ('a -> 'b option) -> 'a list -> 'b list * 'a list
 val split_at : int -> 'a list -> 'a list * 'a list
+
+val zip : 'a list -> 'b list -> ('a * 'b) list
+(** Haskell's zip. Like List.combine but does not raise 
+    when the lengthes are not equal. Tail recursive. *)
+
 val unique : 'a list -> 'a list (** Haskell's nub *)
 
 val is_unique : ('a -> 'b) -> 'a list -> ('a * 'a) option