From 36169b72a8a5b9276a36a5dd8bb9db926ae4598e Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Thu, 28 Sep 1995 10:42:18 +0000 Subject: [PATCH] Ajout de List.for_all2 et List.exists2. git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@299 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02 --- stdlib/list.ml | 12 ++++++++++++ stdlib/list.mli | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/stdlib/list.ml b/stdlib/list.ml index 90c38cbc5..f475c0b23 100644 --- a/stdlib/list.ml +++ b/stdlib/list.ml @@ -91,6 +91,18 @@ let rec exists p = function [] -> false | a::l -> p a or exists p l +let rec for_all2 p l1 l2 = + match (l1, l2) with + ([], []) -> true + | (a1::l1, a2::l2) -> p a1 a2 & for_all2 p l1 l2 + | (_, _) -> invalid_arg "List.for_all2" + +let rec exists2 p l1 l2 = + match (l1, l2) with + ([], []) -> true + | (a1::l1, a2::l2) -> p a1 a2 or exists2 p l1 l2 + | (_, _) -> invalid_arg "List.exists2" + let rec mem x = function [] -> false | a::l -> a = x or mem x l diff --git a/stdlib/list.mli b/stdlib/list.mli index b1217fc5a..60c0feb15 100644 --- a/stdlib/list.mli +++ b/stdlib/list.mli @@ -77,9 +77,14 @@ val for_all : ('a -> bool) -> 'a list -> bool satisfy the predicate [p]. That is, it returns [(p a1) & (p a2) & ... & (p an)]. *) val exists : ('a -> bool) -> 'a list -> bool - (* [exists p [a1; ...; an]] checks if at least one element of the list - satisfies the predicate [p]. That is, it returns + (* [exists p [a1; ...; an]] checks if at least one element of + the list satisfies the predicate [p]. That is, it returns [(p a1) or (p a2) or ... or (p an)]. *) +val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool +val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool + (* Same as [for_all] and [exists], but for a two-argument predicate. + Raise [Invalid_argument] if the two lists have + different lengths. *) val mem : 'a -> 'a list -> bool (* [mem a l] is true if and only if [a] is equal to an element of [l]. *)