Skip to content

Commit 0e4787b

Browse files
authored
Mention complex numbers as an example why Ord is not a superclass of Num (learnyouahaskell#23)
1 parent b57ef39 commit 0e4787b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

docs/recursion.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ <h1 style="margin-left:-2px">Recursion</h1>
7070
| otherwise = x:replicate' (n-1) x
7171
</pre>
7272
<p>We used guards here instead of patterns because we're testing for a boolean condition. If <span class="fixed">n</span> is less than or equal to 0, return an empty list. Otherwise return a list that has <span class="fixed">x</span> as the first element and then <span class="fixed">x</span> replicated n-1 times as the tail. Eventually, the <span class="fixed">(n-1)</span> part will cause our function to reach the edge condition.</p>
73-
<div class="hintbox"><em>Note:</em> <span class="fixed">Num</span> is not a subclass of <span class="fixed">Ord</span>. That means that what constitutes for a number doesn't really have to adhere to an ordering. So that's why we have to specify both the <span class="fixed">Num</span> and <span class="fixed">Ord</span> class constraints when doing addition or subtraction and also comparison.</div>
73+
<div class="hintbox"><em>Note:</em> <span class="fixed">Num</span> is not a subclass of <span class="fixed">Ord</span>. This is because not every number type has an ordering, e.g. complex numbers aren't ordered. So that's why we have to specify both the <span class="fixed">Num</span> and <span class="fixed">Ord</span> class constraints when doing addition or subtraction and also comparison.</div>
7474
<p>Next up, we'll implement <span class="fixed">take</span>. It takes a certain number of elements from a list. For instance, <span class="fixed">take 3 [5,4,3,2,1]</span> will return <span class="fixed">[5,4,3]</span>. If we try to take 0 or less elements from a list, we get an empty list. Also if we try to take anything from an empty list, we get an empty list. Notice that those are two edge conditions right there. So let's write that out:</p>
7575
<pre name="code" class="haskell:hs">
7676
take' :: (Num i, Ord i) =&gt; i -&gt; [a] -&gt; [a]

0 commit comments

Comments
 (0)