You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>We used guards here instead of patterns because we're testing for a boolean condition. If <spanclass="fixed">n</span> is less than or equal to 0, return an empty list. Otherwise return a list that has <spanclass="fixed">x</span> as the first element and then <spanclass="fixed">x</span> replicated n-1 times as the tail. Eventually, the <spanclass="fixed">(n-1)</span> part will cause our function to reach the edge condition.</p>
73
-
<divclass="hintbox"><em>Note:</em><spanclass="fixed">Num</span> is not a subclass of <spanclass="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 <spanclass="fixed">Num</span> and <spanclass="fixed">Ord</span> class constraints when doing addition or subtraction and also comparison.</div>
73
+
<divclass="hintbox"><em>Note:</em><spanclass="fixed">Num</span> is not a subclass of <spanclass="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 <spanclass="fixed">Num</span> and <spanclass="fixed">Ord</span> class constraints when doing addition or subtraction and also comparison.</div>
74
74
<p>Next up, we'll implement <spanclass="fixed">take</span>. It takes a certain number of elements from a list. For instance, <spanclass="fixed">take 3 [5,4,3,2,1]</span> will return <spanclass="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>
75
75
<prename="code" class="haskell:hs">
76
76
take' :: (Num i, Ord i) => i -> [a] -> [a]
0 commit comments