lecture 2: mention the init function

This commit is contained in:
Joel Kaasinen
2020-09-21 09:11:29 +03:00
parent 4285f701fd
commit b0a3876bbf
+8 -7
View File
@@ -1258,13 +1258,14 @@ Ok, two modules loaded.
<p>The Haskell standard library comes with lots of functions that operate on lists. Here are some of the most important ones, together with their types. We’ll get back to what <code>[a]</code> actually means in a second, but for now you can imagine it means “any list”.</p>
<div class="sourceCode" id="cb99"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb99-1" data-line-number="1">head<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> a <span class="co">-- returns the first element</span></a>
<a class="sourceLine" id="cb99-2" data-line-number="2">tail<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- returns everything except the first element</span></a>
<a class="sourceLine" id="cb99-3" data-line-number="3">take<span class="ot"> ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- returns the n first elements</span></a>
<a class="sourceLine" id="cb99-4" data-line-number="4">drop<span class="ot"> ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- returns everything except the n first elements</span></a>
<a class="sourceLine" id="cb99-5" data-line-number="5"><span class="ot">(++) ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- lists are catenated with the ++ operator</span></a>
<a class="sourceLine" id="cb99-6" data-line-number="6"><span class="ot">(!!) ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> a <span class="co">-- lists are indexed with the !! operator</span></a>
<a class="sourceLine" id="cb99-7" data-line-number="7">reverse<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- reverse a list</span></a>
<a class="sourceLine" id="cb99-8" data-line-number="8">null<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Bool</span> <span class="co">-- is this list empty?</span></a>
<a class="sourceLine" id="cb99-9" data-line-number="9">length<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="co">-- the length of a list</span></a></code></pre></div>
<a class="sourceLine" id="cb99-3" data-line-number="3">init<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- returns everything except the last element</span></a>
<a class="sourceLine" id="cb99-4" data-line-number="4">take<span class="ot"> ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- returns the n first elements</span></a>
<a class="sourceLine" id="cb99-5" data-line-number="5">drop<span class="ot"> ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- returns everything except the n first elements</span></a>
<a class="sourceLine" id="cb99-6" data-line-number="6"><span class="ot">(++) ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- lists are catenated with the ++ operator</span></a>
<a class="sourceLine" id="cb99-7" data-line-number="7"><span class="ot">(!!) ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> a <span class="co">-- lists are indexed with the !! operator</span></a>
<a class="sourceLine" id="cb99-8" data-line-number="8">reverse<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- reverse a list</span></a>
<a class="sourceLine" id="cb99-9" data-line-number="9">null<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Bool</span> <span class="co">-- is this list empty?</span></a>
<a class="sourceLine" id="cb99-10" data-line-number="10">length<span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="co">-- the length of a list</span></a></code></pre></div>
<p>Sidenote: the last two operations (<code>null</code> and <code>length</code>) actually have more generic types, but here I’m pretending that you can only use them on lists.</p>
<p>Lists can be compared with the familiar <code>==</code> operator.</p>
<p>Do you remember this from our first GHCi session?</p>