diff --git a/src/batLazyList.ml b/src/batLazyList.ml index 714b184f6..6c4fe715e 100644 --- a/src/batLazyList.ml +++ b/src/batLazyList.ml @@ -617,6 +617,21 @@ let for_all2 p l1 l2 = | (Cons _, Nil) | (Nil, Cons _) -> raise (Different_list_size "LazyList.for_all2") in aux l1 l2 +let equal eq l1 l2 = + let rec aux l1 l2 = + match (next l1, next l2) with + | (Cons (h1, t1), Cons (h2, t2)) -> eq h1 h2 && (aux t1 t2) + | (Nil, Nil) -> true + | (Cons _, Nil) | (Nil, Cons _) -> false + in aux l1 l2 + +(*$T equal + equal (equal (=)) (init 3 (range 0)) (init 3 (range 0)) + not (equal (equal (=)) (of_list [(of_list [0; 1; 2])]) (of_list [(of_list [0; 42; 2])])) + not (equal (=) (range 0 2) (range 0 3)) + not (equal (=) (range 0 3) (range 0 2)) +*) + let exists2 p l1 l2 = let rec aux l1 l2 = match (next l1, next l2) with diff --git a/src/batLazyList.mli b/src/batLazyList.mli index bbe204aad..324f038b1 100644 --- a/src/batLazyList.mli +++ b/src/batLazyList.mli @@ -582,6 +582,28 @@ val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool @raise Different_list_size if the two lists have different lengths. *) +val equal : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool +(** [equal eq s1 s2] compares elements of [s1] and [s2] pairwise using [eq] + and returns true if all elements pass the test and the lists have the same + length; otherwise it returns false. Examples: + + {[ + equal (=) (range 0 4) (range 0 4) (* true *) + + (* Make lazy lists of lazy lists: *) + let s1 = init 5 (range 0) + let s2 = init 5 (range 0) + equal (equal (=)) s1 s2 (* true *) + ]} + + (Calling [=] directly on a pair of lazy lists may succeed but is not + guaranteed to behave consistently.) + + Note that on lists of equal length, [equal] and [for_all2] can perform + the same function; their intended uses differ, however, as signaled by + behavior on lists of different lengths. +*) + val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool (** Same as {!exists}, but for a two-argument predicate. @raise Different_list_size if the two lists have