Skip to content

Commit

Permalink
update page
Browse files Browse the repository at this point in the history
  • Loading branch information
nadia-polikarpova committed Oct 30, 2023
1 parent edc8320 commit abcd8d7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
40 changes: 30 additions & 10 deletions docs/lectures/03-datatypes.html
Original file line number Diff line number Diff line change
Expand Up @@ -1200,10 +1200,30 @@ <h2 id="polymorphic-functions-over-polymorphic-data">Polymorphic Functions over
<span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a><span class="dt">Cons</span> <span class="dv">1</span> (<span class="dt">Cons</span> <span class="dv">2</span> (<span class="dt">Cons</span> <span class="dv">3</span> <span class="dt">Nil</span>))</span>
<span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a><span class="op">&gt;&gt;&gt;</span> append (<span class="dt">Cons</span> <span class="ch">'a'</span> (<span class="dt">Cons</span> <span class="ch">'b'</span> <span class="dt">Nil</span>)) (<span class="dt">Cons</span> <span class="ch">'c'</span> <span class="dt">Nil</span>) <span class="co">-- a = Char</span></span>
<span id="cb59-5"><a href="#cb59-5" aria-hidden="true" tabindex="-1"></a><span class="dt">Cons</span> <span class="ch">'a'</span> (<span class="dt">Cons</span> <span class="ch">'b'</span> (<span class="dt">Cons</span> <span class="ch">'c'</span> <span class="dt">Nil</span>))</span>
<span id="cb59-6"><a href="#cb59-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-7"><a href="#cb59-7" aria-hidden="true" tabindex="-1"></a><span class="op">&gt;&gt;&gt;</span> append (<span class="dt">Cons</span> <span class="fl">1.1</span> (<span class="dt">Cons</span> <span class="fl">2.2</span> <span class="dt">Nil</span>)) (<span class="dt">Cons</span> <span class="ch">'a'</span> <span class="dt">Nil</span>) <span class="co">-- a = ?</span></span>
<span id="cb59-8"><a href="#cb59-8" aria-hidden="true" tabindex="-1"></a><span class="op">???</span></span></code></pre></div>
<span id="cb59-5"><a href="#cb59-5" aria-hidden="true" tabindex="-1"></a><span class="dt">Cons</span> <span class="ch">'a'</span> (<span class="dt">Cons</span> <span class="ch">'b'</span> (<span class="dt">Cons</span> <span class="ch">'c'</span> <span class="dt">Nil</span>))</span></code></pre></div>
<p><br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br></p>
<h2 id="quiz-6">QUIZ</h2>
<p>With the type of <code class="sourceCode haskell">append</code> defined as</p>
<p><code class="sourceCode haskell"><span class="ot">append ::</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> <span class="dt">List</span> a</code></p>
<p>what would be result of</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a>append (<span class="dt">Cons</span> <span class="ch">'a'</span> (<span class="dt">Cons</span> <span class="ch">'b'</span> <span class="dt">Nil</span>)) (<span class="dt">Cons</span> <span class="dt">True</span> <span class="dt">Nil</span>)</span></code></pre></div>
<p><strong>A.</strong> <code class="sourceCode haskell"><span class="dt">Cons</span> <span class="ch">'a'</span> (<span class="dt">Cons</span> <span class="ch">'b'</span> (<span class="dt">Cons</span> <span class="dt">True</span> <span class="dt">Nil</span>))</code></p>
<p><strong>B.</strong> <code class="sourceCode haskell"><span class="dt">Cons</span> <span class="ch">'a'</span> (<span class="dt">Cons</span> <span class="ch">'b'</span> <span class="dt">Nil</span>)</code></p>
<p><strong>C.</strong> Type error</p>
<p><em>Answer:</em> C</p>
<p><br>
<br>
<br>
Expand All @@ -1220,18 +1240,18 @@ <h2 id="polymorphic-functions-over-polymorphic-data">Polymorphic Functions over
<br></p>
<h2 id="built-in-lists">Built-in Lists?</h2>
<p>This is exactly how Haskell’s “built-in” lists are defined:</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> [a] <span class="ot">=</span> [] <span class="op">|</span> (<span class="op">:</span>) a [a]</span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">List</span> a <span class="ot">=</span> <span class="dt">Nil</span> <span class="op">|</span> <span class="dt">Cons</span> a (<span class="dt">List</span> a)</span></code></pre></div>
<div class="sourceCode" id="cb61"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> [a] <span class="ot">=</span> [] <span class="op">|</span> (<span class="op">:</span>) a [a]</span>
<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-3"><a href="#cb61-3" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">List</span> a <span class="ot">=</span> <span class="dt">Nil</span> <span class="op">|</span> <span class="dt">Cons</span> a (<span class="dt">List</span> a)</span></code></pre></div>
<ul>
<li><code class="sourceCode haskell"><span class="dt">Nil</span></code> is called <code class="sourceCode haskell">[]</code></li>
<li><code class="sourceCode haskell"><span class="dt">Cons</span></code> is called <code class="sourceCode haskell"><span class="op">:</span></code></li>
</ul>
<p>Many list manipulating functions e.g. in <code class="sourceCode haskell"><span class="dt">Data.List</span></code> are <em>polymorphic</em>
- Can be reused across all kinds of lists.</p>
<div class="sourceCode" id="cb61"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="ot">(++) ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a]</span>
<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span><span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> a</span>
<span id="cb61-3"><a href="#cb61-3" aria-hidden="true" tabindex="-1"></a><span class="fu">tail</span><span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> [a]</span></code></pre></div>
<div class="sourceCode" id="cb62"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="ot">(++) ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a]</span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span><span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> a</span>
<span id="cb62-3"><a href="#cb62-3" aria-hidden="true" tabindex="-1"></a><span class="fu">tail</span><span class="ot"> ::</span> [a] <span class="ot">-&gt;</span> [a]</span></code></pre></div>
<p><br>
<br>
<br>
Expand Down
38 changes: 36 additions & 2 deletions lectures/03-datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1416,11 +1416,45 @@ Cons 1 (Cons 2 (Cons 3 Nil))
>>> append (Cons 'a' (Cons 'b' Nil)) (Cons 'c' Nil) -- a = Char
Cons 'a' (Cons 'b' (Cons 'c' Nil))
```

>>> append (Cons 1.1 (Cons 2.2 Nil)) (Cons 'a' Nil) -- a = ?
???
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

## QUIZ

With the type of `append` defined as

`append :: List a -> List a -> List a`

what would be result of

```haskell
append (Cons 'a' (Cons 'b' Nil)) (Cons True Nil)
```

**A.** `Cons 'a' (Cons 'b' (Cons True Nil))`

**B.** `Cons 'a' (Cons 'b' Nil)`

**C.** Type error

(I) final

*Answer:* C

<br>
<br>
<br>
Expand Down

0 comments on commit abcd8d7

Please sign in to comment.