1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
Module Array: array operations
</TITLE>
</HEAD>
<BODY >
<A HREF="manual031.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual033.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>
<H2>17.2 Module <TT>Array</TT>: array operations</H2><A NAME="s:Array"></A>
<A NAME="@manual160"></A><PRE>
val length : 'a array -> int
</PRE>
<A NAME="@manual161"></A><BLOCKQUOTE>
Return the length (number of elements) of the given array.
</BLOCKQUOTE>
<PRE>
val get: 'a array -> int -> 'a
</PRE>
<A NAME="@manual162"></A><BLOCKQUOTE>
<CODE>Array.get a n</CODE> returns the element number <CODE>n</CODE> of array <CODE>a</CODE>.
The first element has number 0.
The last element has number <CODE>Array.length a - 1</CODE>.
Raise <CODE>Invalid_argument "Array.get"</CODE> if <CODE>n</CODE> is outside the range
0 to <CODE>(Array.length a - 1)</CODE>.
You can also write <CODE>a.(n)</CODE> instead of <CODE>Array.get a n</CODE>.
</BLOCKQUOTE>
<PRE>
val set: 'a array -> int -> 'a -> unit
</PRE>
<A NAME="@manual163"></A><BLOCKQUOTE>
<CODE>Array.set a n x</CODE> modifies array <CODE>a</CODE> in place, replacing
element number <CODE>n</CODE> with <CODE>x</CODE>.
Raise <CODE>Invalid_argument "Array.set"</CODE> if <CODE>n</CODE> is outside the range
0 to <CODE>Array.length a - 1</CODE>.
You can also write <CODE>a.(n) <- x</CODE> instead of <CODE>Array.set a n x</CODE>.
</BLOCKQUOTE>
<PRE>
val make: int -> 'a -> 'a array
val create: int -> 'a -> 'a array
</PRE>
<A NAME="@manual164"></A><A NAME="@manual165"></A><BLOCKQUOTE>
<CODE>Array.make n x</CODE> returns a fresh array of length <CODE>n</CODE>,
initialized with <CODE>x</CODE>.
All the elements of this new array are initially
physically equal to <CODE>x</CODE> (in the sense of the <CODE>==</CODE> predicate).
Consequently, if <CODE>x</CODE> is mutable, it is shared among all elements
of the array, and modifying <CODE>x</CODE> through one of the array entries
will modify all other entries at the same time.
Raise <CODE>Invalid_argument</CODE> if <CODE>n <= 0</CODE> or <CODE>n > Sys.max_array_length</CODE>.
If the value of <CODE>x</CODE> is a floating-point number, then the maximum
size is only <CODE>Sys.max_array_length / 2</CODE>.
<CODE>Array.create</CODE> is a deprecated alias for <CODE>Array.make</CODE>.
</BLOCKQUOTE>
<PRE>
val init: int -> (int -> 'a) -> 'a array
</PRE>
<A NAME="@manual166"></A><BLOCKQUOTE>
<CODE>Array.init n f</CODE> returns a fresh array of length <CODE>n</CODE>,
with element number <CODE>i</CODE> initialized to the result of <CODE>f i</CODE>.
In other terms, <CODE>Array.init n f</CODE> tabulates the results of <CODE>f</CODE>
applied to the integers <CODE>0</CODE> to <CODE>n-1</CODE>.
</BLOCKQUOTE>
<PRE>
val make_matrix: int -> int -> 'a -> 'a array array
val create_matrix: int -> int -> 'a -> 'a array array
</PRE>
<A NAME="@manual167"></A><A NAME="@manual168"></A><BLOCKQUOTE>
<CODE>Array.make_matrix dimx dimy e</CODE> returns a two-dimensional array
(an array of arrays) with first dimension <CODE>dimx</CODE> and
second dimension <CODE>dimy</CODE>. All the elements of this new matrix
are initially physically equal to <CODE>e</CODE>.
The element (<CODE>x,y</CODE>) of a matrix <CODE>m</CODE> is accessed
with the notation <CODE>m.(x).(y)</CODE>.
Raise <CODE>Invalid_argument</CODE> if <CODE>dimx</CODE> or <CODE>dimy</CODE> is less than 1 or
greater than <CODE>Sys.max_array_length</CODE>.
If the value of <CODE>e</CODE> is a floating-point number, then the maximum
size is only <CODE>Sys.max_array_length / 2</CODE>.
<CODE>Array.create_matrix</CODE> is a deprecated alias for <CODE>Array.make_matrix</CODE>.</BLOCKQUOTE>
<PRE>
val append: 'a array -> 'a array -> 'a array
</PRE>
<A NAME="@manual169"></A><BLOCKQUOTE>
<CODE>Array.append v1 v2</CODE> returns a fresh array containing the
concatenation of the arrays <CODE>v1</CODE> and <CODE>v2</CODE>.
</BLOCKQUOTE>
<PRE>
val concat: 'a array list -> 'a array
</PRE>
<A NAME="@manual170"></A><BLOCKQUOTE>
Same as <CODE>Array.append</CODE>, but catenates a list of arrays.
</BLOCKQUOTE>
<PRE>
val sub: 'a array -> int -> int -> 'a array
</PRE>
<A NAME="@manual171"></A><BLOCKQUOTE>
<CODE>Array.sub a start len</CODE> returns a fresh array of length <CODE>len</CODE>,
containing the elements number <CODE>start</CODE> to <CODE>start + len - 1</CODE>
of array <CODE>a</CODE>.
Raise <CODE>Invalid_argument "Array.sub"</CODE> if <CODE>start</CODE> and <CODE>len</CODE> do not
designate a valid subarray of <CODE>a</CODE>; that is, if
<CODE>start < 0</CODE>, or <CODE>len < 0</CODE>, or <CODE>start + len > Array.length a</CODE>.
</BLOCKQUOTE>
<PRE>
val copy: 'a array -> 'a array
</PRE>
<A NAME="@manual172"></A><BLOCKQUOTE>
<CODE>Array.copy a</CODE> returns a copy of <CODE>a</CODE>, that is, a fresh array
containing the same elements as <CODE>a</CODE>.
</BLOCKQUOTE>
<PRE>
val fill: 'a array -> int -> int -> 'a -> unit
</PRE>
<A NAME="@manual173"></A><BLOCKQUOTE>
<CODE>Array.fill a ofs len x</CODE> modifies the array <CODE>a</CODE> in place,
storing <CODE>x</CODE> in elements number <CODE>ofs</CODE> to <CODE>ofs + len - 1</CODE>.
Raise <CODE>Invalid_argument "Array.fill"</CODE> if <CODE>ofs</CODE> and <CODE>len</CODE> do not
designate a valid subarray of <CODE>a</CODE>.
</BLOCKQUOTE>
<PRE>
val blit: 'a array -> int -> 'a array -> int -> int -> unit
</PRE>
<A NAME="@manual174"></A><BLOCKQUOTE>
<CODE>Array.blit v1 o1 v2 o2 len</CODE> copies <CODE>len</CODE> elements
from array <CODE>v1</CODE>, starting at element number <CODE>o1</CODE>, to array <CODE>v2</CODE>,
starting at element number <CODE>o2</CODE>. It works correctly even if
<CODE>v1</CODE> and <CODE>v2</CODE> are the same array, and the source and
destination chunks overlap.
Raise <CODE>Invalid_argument "Array.blit"</CODE> if <CODE>o1</CODE> and <CODE>len</CODE> do not
designate a valid subarray of <CODE>v1</CODE>, or if <CODE>o2</CODE> and <CODE>len</CODE> do not
designate a valid subarray of <CODE>v2</CODE>.
</BLOCKQUOTE>
<PRE>
val to_list: 'a array -> 'a list
</PRE>
<A NAME="@manual175"></A><BLOCKQUOTE>
<CODE>Array.to_list a</CODE> returns the list of all the elements of <CODE>a</CODE>.
</BLOCKQUOTE>
<PRE>
val of_list: 'a list -> 'a array
</PRE>
<A NAME="@manual176"></A><BLOCKQUOTE>
<CODE>Array.of_list l</CODE> returns a fresh array containing the elements
of <CODE>l</CODE>.
</BLOCKQUOTE>
<PRE>
val iter: ('a -> unit) -> 'a array -> unit
</PRE>
<A NAME="@manual177"></A><BLOCKQUOTE>
<CODE>Array.iter f a</CODE> applies function <CODE>f</CODE> in turn to all
the elements of <CODE>a</CODE>. It is equivalent to
<CODE>f a.(0); f a.(1); ...; f a.(Array.length a - 1); ()</CODE>.
</BLOCKQUOTE>
<PRE>
val map: ('a -> 'b) -> 'a array -> 'b array
</PRE>
<A NAME="@manual178"></A><BLOCKQUOTE>
<CODE>Array.map f a</CODE> applies function <CODE>f</CODE> to all the elements of <CODE>a</CODE>,
and builds an array with the results returned by <CODE>f</CODE>:
<CODE>[| f a.(0); f a.(1); ...; f a.(Array.length a - 1) |]</CODE>.
</BLOCKQUOTE>
<PRE>
val iteri: (int -> 'a -> unit) -> 'a array -> unit
val mapi: (int -> 'a -> 'b) -> 'a array -> 'b array
</PRE>
<A NAME="@manual179"></A><A NAME="@manual180"></A><BLOCKQUOTE>
Same as <CODE>Array.iter</CODE> and <CODE>Array.map</CODE> respectively, but the
function is applied to the index of the element as first argument,
and the element itself as second argument.
</BLOCKQUOTE>
<PRE>
val fold_left: ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
</PRE>
<A NAME="@manual181"></A><BLOCKQUOTE>
<CODE>Array.fold_left f x a</CODE> computes
<CODE>f (... (f (f x a.(0)) a.(1)) ...) a.(n-1)</CODE>,
where <CODE>n</CODE> is the length of the array <CODE>a</CODE>.
</BLOCKQUOTE>
<PRE>
val fold_right: ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a
</PRE>
<A NAME="@manual182"></A><BLOCKQUOTE>
<CODE>Array.fold_right f a x</CODE> computes
<CODE>f a.(0) (f a.(1) ( ... (f a.(n-1) x) ...))</CODE>,
where <CODE>n</CODE> is the length of the array <CODE>a</CODE>.
</BLOCKQUOTE>
<HR>
<A HREF="manual031.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual033.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>
|