summaryrefslogtreecommitdiff
path: root/stdlib/array.ml
diff options
context:
space:
mode:
authorBernhard Schommer <bernhardschommer@gmail.com>2015-12-04 10:10:36 +0100
committerBernhard Schommer <bernhardschommer@gmail.com>2015-12-04 10:10:36 +0100
commitdcc1421e1e1e7439b59629996bc70d6f42774e6a (patch)
tree4eba786bebd487e0dc984a2e9da57c6dd34881df /stdlib/array.ml
parent7f23ad6692ff512af2e7874a72d116fafc706042 (diff)
downloadocaml-dcc1421e1e1e7439b59629996bc70d6f42774e6a.tar.gz
Added exists2, for_all2, mem, memq and find.
The functions are implemented as their pendants in the List module.
Diffstat (limited to 'stdlib/array.ml')
-rw-r--r--stdlib/array.ml44
1 files changed, 44 insertions, 0 deletions
diff --git a/stdlib/array.ml b/stdlib/array.ml
index d327f889bf..89848fdcd8 100644
--- a/stdlib/array.ml
+++ b/stdlib/array.ml
@@ -150,6 +150,50 @@ let for_all p a =
else false in
loop 0
+let for_all2 p l1 l2 =
+ let n1 = length l1
+ and n2 = length l2 in
+ if n1 <> n2 then invalid_arg "Array.for_all2"
+ else let rec loop i =
+ if i = n1 then true
+ else if p (unsafe_get l1 i) (unsafe_get l2 i) then loop (succ i)
+ else false in
+ loop 0
+
+let exists2 p l1 l2 =
+ let n1 = length l1
+ and n2 = length l2 in
+ if n1 <> n2 then invalid_arg "Array.exists2"
+ else let rec loop i =
+ if i = n1 then false
+ else if p (unsafe_get l1 i) (unsafe_get l2 i) then true
+ else loop (succ i) in
+ loop 0
+
+let mem x a =
+ let n = length a in
+ let rec loop i =
+ if i = n then false
+ else if compare (unsafe_get a i) x == 0 then true
+ else loop (succ i) in
+ loop 0
+
+let memq x a =
+ let n = length a in
+ let rec loop i =
+ if i = n then false
+ else if x == (unsafe_get a i) then true
+ else loop (succ i) in
+ loop 0
+
+let find p a =
+ let n = length a in
+ let rec loop i =
+ if i = n then raise Not_found
+ else if p (unsafe_get a i) then (unsafe_get a i)
+ else loop (succ i) in
+ loop 0
+
exception Bottom of int;;
let sort cmp a =
let maxson l i =