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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">
<title>ForLoops - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">
<link rel="Start" href="Home">
</head>
<body lang="en" dir="ltr">
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
<tr>
<td style = "
border: 0px;
color: darkblue;
font-size: 150%;
text-align: left;">
<a class = mltona href="Home">MLton 20061025</a>
<td style = "
border: 0px;
font-size: 150%;
text-align: center;
width: 50%;">
ForLoops
<td style = "
border: 0px;
text-align: right;">
<table cellspacing = 0 style = "border: 0px">
<tr style = "vertical-align: middle;">
</table>
<tr style = "background-color: white;">
<td colspan = 3
style = "
border: 0px;
font-size:70%;
text-align: right;">
<a href = "Home">Home</a>
<a href = "Index">Index</a>
</table>
<div id="content" lang="en" dir="ltr">
A <tt>for</tt>-loop is typically used to iterate over a range of consecutive integers that denote indices of some sort. For example, in <a href="OCaml">OCaml</a> a <tt>for</tt>-loop takes either the form
<pre>for <name> = <lower> to <upper> do <body> done
</pre>or the form
<pre>for <name> = <upper> downto <lower> do <body> done
</pre>Some languages provide considerably more flexible <tt>for</tt>-loop or <tt>foreach</tt>-constructs. <p>
A bit surprisingly, <a href="StandardML">Standard ML</a> provides special syntax for <tt>while</tt>-loops, but not for <tt>for</tt>-loops. Indeed, in SML, many uses of <tt>for</tt>-loops are better expressed using <tt>app</tt>, <tt>foldl/foldr</tt>, <tt>map</tt> and many other higher-order functions provided by the <a href="BasisLibrary">Basis Library</a> for manipulating lists, vectors and arrays. However, the Basis Library does not provide a function for iterating over a range of integer values. Fortunately, it is very easy to write one.
</p>
<h2 id="head-256b31fc11b749803c8295d4ec8da28712f04f19">A fairly simple design</h2>
<p>
The following implementation imitates both the syntax and semantics of the OCaml <tt>for</tt>-loop.
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B> for <B><FONT COLOR="#5F9EA0">=</FONT></B> to <B><FONT COLOR="#A020F0">of</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#5F9EA0">*</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>
<B><FONT COLOR="#5F9EA0">|</FONT></B> downto <B><FONT COLOR="#A020F0">of</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#5F9EA0">*</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>
<B><FONT COLOR="#A020F0">infix</FONT></B> to downto
<B><FONT COLOR="#A020F0">val</FONT></B> for <B><FONT COLOR="#5F9EA0">=</FONT></B>
<B><FONT COLOR="#A020F0">fn</FONT></B> lo to up <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B>
(<B><FONT COLOR="#A020F0">fn</FONT></B> f <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#A020F0">let</FONT></B><B><FONT COLOR="#A020F0"> fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">loop</FONT></I></B></FONT></B> lo <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> lo <B><FONT COLOR="#5F9EA0">></FONT></B> up <B><FONT COLOR="#A020F0">then</FONT></B> ()
<B><FONT COLOR="#A020F0">else</FONT></B> (f lo; loop (lo<B><FONT COLOR="#5F9EA0">+</FONT></B>1))
<B><FONT COLOR="#A020F0">in</FONT></B> loop lo <B><FONT COLOR="#A020F0">end</FONT></B>)
<B><FONT COLOR="#5F9EA0">|</FONT></B> up downto lo <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B>
(<B><FONT COLOR="#A020F0">fn</FONT></B> f <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#A020F0">let</FONT></B><B><FONT COLOR="#A020F0"> fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">loop</FONT></I></B></FONT></B> up <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> up < lo <B><FONT COLOR="#A020F0">then</FONT></B> ()
<B><FONT COLOR="#A020F0">else</FONT></B> (f up; loop (up<B><FONT COLOR="#5F9EA0">-</FONT></B>1))
<B><FONT COLOR="#A020F0">in</FONT></B> loop up <B><FONT COLOR="#A020F0">end</FONT></B>)
</PRE>
<p>
</p>
<p>
For example,
<pre class=code>
for (1 to 9)
(<B><FONT COLOR="#A020F0">fn</FONT></B> i <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#A020F0">print</FONT></B> (Int.toString i))
</PRE>
would print <tt>123456789</tt> and
<pre class=code>
for (9 downto 1)
(<B><FONT COLOR="#A020F0">fn</FONT></B> i <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#A020F0">print</FONT></B> (Int.toString i))
</PRE>
would print <tt>987654321</tt>.
</p>
<p>
Straightforward formatting of nested loops
<pre class=code>
for (a to b)
(<B><FONT COLOR="#A020F0">fn</FONT></B> i <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B>
for (c to d)
(<B><FONT COLOR="#A020F0">fn</FONT></B> j <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B>
...))
</PRE>
is fairly readable, but tends to cause the body of the loop to be indented quite deeply.
</p>
<h2 id="head-db5cb13e652c830a05ed522c82b38855adf3f29d">Off-by-one</h2>
<p>
The above design has an annoying feature. In practice, the upper bound of the iterated range is almost always excluded and most loops would subtract one from the upper bound:
<pre class=code>
for (0 to n<B><FONT COLOR="#5F9EA0">-</FONT></B>1) ...
for (n<B><FONT COLOR="#5F9EA0">-</FONT></B>1 downto 0) ...
</PRE>
It is probably better to break convention and exclude the upper bound by default, because it leads to more concise code and becomes idiomatic with very little practise. The iterator combinators described below exclude the upper bound by default.
</p>
<h2 id="head-a43056e62b4e6692169c07351860dd6bb04042d0">Iterator combinators</h2>
<p>
While the simple <tt>for</tt>-function described in the previous section is probably good enough for many uses, it is a bit cumbersome when one needs to iterate over a cartesian product. One might also want to iterate over more than just consecutive integers. It turns out that one can provide a library of iterator combinators that allow one to implement iterators more flexibly.
</p>
<p>
Since the types of the combinators may be a bit difficult to infer from their implementations, let's first take a look at a signature of the iterator combinator library:
<pre class=code>
<B><FONT COLOR="#5F9EA0">signature</FONT></B> ITER <B><FONT COLOR="#5F9EA0">=</FONT></B>
<B><FONT COLOR="#5F9EA0">sig</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B> 'a t <B><FONT COLOR="#5F9EA0">=</FONT></B> ('a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> return : 'a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#5F9EA0">></FONT></B><B><FONT COLOR="#5F9EA0">>=</FONT></B> : 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> ('a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'b t) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'b t
<B><FONT COLOR="#A020F0">val</FONT></B> none : 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> to : <B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#5F9EA0">*</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> t
<B><FONT COLOR="#A020F0">val</FONT></B> downto : <B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#5F9EA0">*</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> t
<B><FONT COLOR="#A020F0">val</FONT></B> inList : 'a <B><FONT COLOR="#228B22">list</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> inVector : 'a <B><FONT COLOR="#228B22">vector</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> inArray : 'a <B><FONT COLOR="#228B22">array</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> using : ('a, 'b) StringCvt.reader <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'b <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> when : 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> ('a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#228B22">bool</FONT></B>) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> by : 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> ('a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'b) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'b t
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#5F9EA0">@</FONT></B><B><FONT COLOR="#5F9EA0">@</FONT></B> : 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> 'a t <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#5F9EA0">*</FONT></B><B><FONT COLOR="#5F9EA0">*</FONT></B> : 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> 'b t <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> ('a, 'b) product t
<B><FONT COLOR="#A020F0">val</FONT></B> for : 'a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> 'a
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
</p>
<p>
Several of the above combinators are meant to be used as infix operators. Here is a set of suitable infix declarations:
<pre class=code>
<B><FONT COLOR="#A020F0">infix</FONT></B> 2 to downto
<B><FONT COLOR="#A020F0">infix</FONT></B> 1 <B><FONT COLOR="#5F9EA0">@</FONT></B><B><FONT COLOR="#5F9EA0">@</FONT></B> when by
<B><FONT COLOR="#A020F0">infix</FONT></B> 0 <B><FONT COLOR="#5F9EA0">></FONT></B><B><FONT COLOR="#5F9EA0">>=</FONT></B> <B><FONT COLOR="#5F9EA0">*</FONT></B><B><FONT COLOR="#5F9EA0">*</FONT></B>
</PRE>
</p>
<p>
A few notes are in order:
</p>
<ul>
<li>
<p>
The <tt>'a t</tt> type constructor with the <tt>return</tt> and <tt>>>=</tt> operators forms a monad.
</p>
</li>
<li>
<p>
The <tt>to</tt> and <tt>downto</tt> combinators will omit the upper bound of the range.
</p>
</li>
<li>
<p>
<tt>for</tt> is the identity function. It is purely for syntactic sugar and is not strictly required.
</p>
</li>
<li>
<p>
The <tt>@@</tt> combinator produces an iterator for the concatenation of the given iterators.
</p>
</li>
<li>
<p>
The <tt>**</tt> combinator produces an iterator for the cartesian product of the given iterators.
</p>
</li>
<ul>
<li>
<p>
See <a href="ProductType">ProductType</a> for the type constructor <tt>('a, 'b) product</tt> used in the type of the iterator produced by <tt>**</tt>.
</p>
</li>
</ul>
<li>
<p>
The <tt>using</tt> combinator allows one to iterate over slices, streams and many other kinds of sequences.
</p>
</li>
<li>
<p>
<tt>when</tt> is the filtering combinator. The name <tt>when</tt> is inspired by <a href="OCaml">OCaml</a>'s guard clauses.
</p>
</li>
<li>
<p>
<tt>by</tt> is the mapping combinator.
</p>
</li>
</ul>
<p>
The below implementation of the <tt>ITER</tt>-signature makes use of the following basic combinators:
<pre class=code>
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">const</FONT></I></B></FONT></B> x _ <B><FONT COLOR="#5F9EA0">=</FONT></B> x
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">flip</FONT></I></B></FONT></B> f x y <B><FONT COLOR="#5F9EA0">=</FONT></B> f y x
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">id</FONT></I></B></FONT></B> x <B><FONT COLOR="#5F9EA0">=</FONT></B> x
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">opt</FONT></I></B></FONT></B> fno fso <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">fn</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> fno () <B><FONT COLOR="#5F9EA0">|</FONT></B> <B><FONT COLOR="#5F9EA0">SOME</FONT></B> ? <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> fso ?
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">pass</FONT></I></B></FONT></B> x f <B><FONT COLOR="#5F9EA0">=</FONT></B> f x
</PRE>
</p>
<p>
Here is an implementation the <tt>ITER</tt>-signature:
<pre class=code>
<B><FONT COLOR="#5F9EA0">structure</FONT></B> Iter :<B><FONT COLOR="#5F9EA0">></FONT></B> ITER <B><FONT COLOR="#5F9EA0">=</FONT></B>
<B><FONT COLOR="#5F9EA0">struct</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B> 'a t <B><FONT COLOR="#5F9EA0">=</FONT></B> ('a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> return <B><FONT COLOR="#5F9EA0">=</FONT></B> pass
<B><FONT COLOR="#A020F0">fun</FONT></B> (iA <B><FONT COLOR="#5F9EA0">></FONT></B><B><FONT COLOR="#5F9EA0">>=</FONT></B> a2iB) f <B><FONT COLOR="#5F9EA0">=</FONT></B> iA (flip a2iB f)
<B><FONT COLOR="#A020F0">val</FONT></B> none <B><FONT COLOR="#5F9EA0">=</FONT></B> ignore
<B><FONT COLOR="#A020F0">fun</FONT></B> (l to u) f <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">fun</FONT></B> `l <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> l<u <B><FONT COLOR="#A020F0">then</FONT></B> (f l; `(l<B><FONT COLOR="#5F9EA0">+</FONT></B>1)) <B><FONT COLOR="#A020F0">else</FONT></B> () <B><FONT COLOR="#A020F0">in</FONT></B> `l <B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> (u downto l) f <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">fun</FONT></B> `u <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> u<B><FONT COLOR="#5F9EA0">></FONT></B>l <B><FONT COLOR="#A020F0">then</FONT></B> (f (u<B><FONT COLOR="#5F9EA0">-</FONT></B>1); `(u<B><FONT COLOR="#5F9EA0">-</FONT></B>1)) <B><FONT COLOR="#A020F0">else</FONT></B> () <B><FONT COLOR="#A020F0">in</FONT></B> `u <B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0"> fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">inList</FONT></I></B></FONT></B> ? <B><FONT COLOR="#5F9EA0">=</FONT></B> flip List.app ?
<B><FONT COLOR="#A020F0"> fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">inVector</FONT></I></B></FONT></B> ? <B><FONT COLOR="#5F9EA0">=</FONT></B> flip Vector.app ?
<B><FONT COLOR="#A020F0"> fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">inArray</FONT></I></B></FONT></B> ? <B><FONT COLOR="#5F9EA0">=</FONT></B> flip Array.app ?
<B><FONT COLOR="#A020F0"> fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">using</FONT></I></B></FONT></B> get s f <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">fun</FONT></B> `s <B><FONT COLOR="#5F9EA0">=</FONT></B> opt (const ()) (<B><FONT COLOR="#A020F0">fn</FONT></B> (x, s) <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> (f x; `s)) (get s) <B><FONT COLOR="#A020F0">in</FONT></B> `s <B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> (iA when p) f <B><FONT COLOR="#5F9EA0">=</FONT></B> iA (<B><FONT COLOR="#A020F0">fn</FONT></B> a <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> p a <B><FONT COLOR="#A020F0">then</FONT></B> f a <B><FONT COLOR="#A020F0">else</FONT></B> ())
<B><FONT COLOR="#A020F0">fun</FONT></B> (iA by g) f <B><FONT COLOR="#5F9EA0">=</FONT></B> iA (f <B><FONT COLOR="#A020F0">o</FONT></B> g)
<B><FONT COLOR="#A020F0">fun</FONT></B> (iA <B><FONT COLOR="#5F9EA0">@</FONT></B><B><FONT COLOR="#5F9EA0">@</FONT></B> iB) f <B><FONT COLOR="#5F9EA0">=</FONT></B> (iA f : <B><FONT COLOR="#228B22">unit</FONT></B>; iB f)
<B><FONT COLOR="#A020F0">fun</FONT></B> (iA <B><FONT COLOR="#5F9EA0">*</FONT></B><B><FONT COLOR="#5F9EA0">*</FONT></B> iB) f <B><FONT COLOR="#5F9EA0">=</FONT></B> iA (<B><FONT COLOR="#A020F0">fn</FONT></B> a <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> iB (<B><FONT COLOR="#A020F0">fn</FONT></B> b <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> f (a & b)))
<B><FONT COLOR="#A020F0">val</FONT></B> for <B><FONT COLOR="#5F9EA0">=</FONT></B> id
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
</p>
<p>
Note that some of the above combinators (e.g. <tt>**</tt>) could be expressed in terms of the other combinators, most notably <tt>return</tt> and <tt>>>=</tt>. Another implementation issue worth mentioning is that <tt>downto</tt> is written specifically to avoid computing <tt>l-1</tt>, which could cause an <tt>Overflow</tt>.
</p>
<p>
To use the above combinators the <tt>Iter</tt>-structure needs to be opened
<pre class=code>
<B><FONT COLOR="#A020F0">open</FONT></B> Iter
</PRE>
and one usually also wants to declare the infix status of the operators as shown earlier.
</p>
<p>
Here is an example that illustrates some of the features:
<pre class=code>
for (0 to 10 when (<B><FONT COLOR="#A020F0">fn</FONT></B> x <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> x <B><FONT COLOR="#5F9EA0">mod</FONT></B> 3 <B><FONT COLOR="#5F9EA0"><></FONT></B> 0) <B><FONT COLOR="#5F9EA0">*</FONT></B><B><FONT COLOR="#5F9EA0">*</FONT></B> inList [<B><FONT COLOR="#BC8F8F">"a"</FONT></B>, <B><FONT COLOR="#BC8F8F">"b"</FONT></B>] <B><FONT COLOR="#5F9EA0">*</FONT></B><B><FONT COLOR="#5F9EA0">*</FONT></B> 2 downto 1 by <B><FONT COLOR="#228B22">real</FONT></B>)
(<B><FONT COLOR="#A020F0">fn</FONT></B> x & y & z <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B>
<B><FONT COLOR="#A020F0">print</FONT></B> (<B><FONT COLOR="#BC8F8F">"("</FONT></B><B><FONT COLOR="#5F9EA0">^</FONT></B>Int.toString x<B><FONT COLOR="#5F9EA0">^</FONT></B><B><FONT COLOR="#BC8F8F">", \""</FONT></B><B><FONT COLOR="#5F9EA0">^</FONT></B>y<B><FONT COLOR="#5F9EA0">^</FONT></B><B><FONT COLOR="#BC8F8F">"\", "</FONT></B><B><FONT COLOR="#5F9EA0">^</FONT></B>Real.toString z<B><FONT COLOR="#5F9EA0">^</FONT></B><B><FONT COLOR="#BC8F8F">")\n"</FONT></B>))
</PRE>
</p>
<p>
Using the <tt>Iter</tt> combinators one can easily produce more complicated iterators. For example, here is an iterator over a "triangle":
<pre class=code>
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">triangle</FONT></I></B></FONT></B> (l, u) <B><FONT COLOR="#5F9EA0">=</FONT></B> l to u <B><FONT COLOR="#5F9EA0">></FONT></B><B><FONT COLOR="#5F9EA0">>=</FONT></B> (<B><FONT COLOR="#A020F0">fn</FONT></B> i <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> i to u <B><FONT COLOR="#5F9EA0">></FONT></B><B><FONT COLOR="#5F9EA0">>=</FONT></B> (<B><FONT COLOR="#A020F0">fn</FONT></B> j <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> return (i, j)))
</PRE>
</p>
</div>
<p>
<hr>
Last edited on 2006-03-26 14:16:53 by <span title="cs180212.pp.htv.fi"><a href="VesaKarvonen">VesaKarvonen</a></span>.
</body></html>
|