lecture 4: fix typo, prefer indices over indexes

closes #53
This commit is contained in:
Joel Kaasinen
2022-08-08 11:41:24 +03:00
parent 61f6685dd4
commit fe3f59ca16
+2 -2
View File
@@ -2765,8 +2765,8 @@ unspecified
<p>Now we can look at the type of the <code>array</code> function that constructs an array.</p>
<div class="sourceCode" id="cb317"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb317-1"><a href="#cb317-1" aria-hidden="true" tabindex="-1"></a><span class="ot">array ::</span> <span class="dt">Ix</span> i <span class="ot">=&gt;</span> (i, i) <span class="ot">-&gt;</span> [(i, e)] <span class="ot">-&gt;</span> <span class="dt">Array</span> i e</span></code></pre></div>
<p>There are a couple of things to notice here. First of all, the <code>Array</code> type is parameterized by <em>two</em> types: the index type and the element type. Most other programming languages only parameterize arrays with the element type, but the index type is always <code>int</code>. In Haskell, we can have, for example, an <code>Array Char Int</code>: an array indexed by characters, or <code>Array Bool String</code>, an array indexed by booleans, or even <code>Array (Int,Int) Int</code>, a two-dimensional array of ints.</p>
<p>Not all types can be index types. Only types that are similar to integers are suitable. That is the reason for the <code>Ix i</code> class constraint. The <code>Ix</code> class collects all the types that can be used as array indexes.</p>
<p>Secondly, the <code>array</code> function takes an extra <code>(i,i)</code> parameter. These are the minimun and maximum indexes of the array. Unlike some other languages, where arrays always start at index 0 or 1, in Haskell you can define an array that starts from 7 and goes to 11. So here’s that array:</p>
<p>Not all types can be index types. Only types that are similar to integers are suitable. That is the reason for the <code>Ix i</code> class constraint. The <code>Ix</code> class collects all the types that can be used as array indices.</p>
<p>Secondly, the <code>array</code> function takes an extra <code>(i,i)</code> parameter. These are the minimum and maximum indices of the array. Unlike some other languages, where arrays always start at index 0 or 1, in Haskell you can define an array that starts from 7 and goes to 11. So here’s that array:</p>
<div class="sourceCode" id="cb318"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb318-1"><a href="#cb318-1" aria-hidden="true" tabindex="-1"></a><span class="ot">myArray ::</span> <span class="dt">Array</span> <span class="dt">Int</span> <span class="dt">String</span></span>
<span id="cb318-2"><a href="#cb318-2" aria-hidden="true" tabindex="-1"></a>myArray <span class="ot">=</span> array (<span class="dv">7</span>,<span class="dv">11</span>) [(<span class="dv">7</span>,<span class="st">&quot;seven&quot;</span>), (<span class="dv">8</span>,<span class="st">&quot;eight&quot;</span>), (<span class="dv">9</span>,<span class="st">&quot;nine&quot;</span>), (<span class="dv">10</span>,<span class="st">&quot;ten&quot;</span>), (<span class="dv">11</span>,<span class="st">&quot;ELEVEN&quot;</span>)]</span></code></pre></div>
<p>Listing all the indices and elements in order can be a bit cumbersome, so there’s also the <code>listArray</code> constructor that just takes a list of elements in order:</p>