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
sneaky and can act like an integer or a floating-point number.
115
117
<code>4.0</code> can’t act like an integer, so <code>5</code> is the one
116
118
that has to adapt.</p>
119
+
<divclass="hintbox">
120
+
<p><strong>Note:</strong> GHC errors are all assigned unique identifiers
121
+
such as <code>GHC-39999</code> above. Whenever you are stuck with a
122
+
stubborn error, you can look it up at <a
123
+
href="https://errors.haskell.org/">https://errors.haskell.org/</a> to
124
+
learn typical causes and solutions.</p>
125
+
</div>
117
126
<p>You may not have known it but we’ve been using functions now all
118
127
along. For instance, <code>*</code> is a function that takes two numbers
119
128
and multiplies them. As you’ve seen, we call it by sandwiching it
@@ -179,7 +188,8 @@ <h2 id="babys-first-functions">Baby’s first functions</h2>
179
188
<p>In the previous section we got a basic feel for calling functions.
180
189
Now let’s try making our own! Open up your favorite text editor and
181
190
punch in this function that takes a number and multiplies it by two.</p>
182
-
<preclass="haskell: hs"><code>doubleMe x = x + x</code></pre>
191
+
<divclass="sourceCode" id="cb10"><pre
192
+
class="sourceCode haskell: hs"><codeclass="sourceCode haskell"><spanid="cb10-1"><ahref="#cb10-1" aria-hidden="true" tabindex="-1"></a>doubleMe x <spanclass="ot">=</span> x <spanclass="op">+</span> x</span></code></pre></div>
183
193
<p>Functions are defined in a similar way that they are called. The
184
194
function name is followed by parameters separated by spaces. But when
185
195
defining functions, there’s a <code>=</code> and after that we define
@@ -198,7 +208,8 @@ <h2 id="babys-first-functions">Baby’s first functions</h2>
198
208
numbers (anything that can be considered a number, really), our function
199
209
also works on any number. Let’s make a function that takes two numbers
200
210
and multiplies each by two and then adds them together.</p>
201
-
<preclass="haskell: hs"><code>doubleUs x y = x*2 + y*2</code></pre>
211
+
<divclass="sourceCode" id="cb12"><pre
212
+
class="sourceCode haskell: hs"><codeclass="sourceCode haskell"><spanid="cb12-1"><ahref="#cb12-1" aria-hidden="true" tabindex="-1"></a>doubleUs x y <spanclass="ot">=</span> x<spanclass="op">*</span><spanclass="dv">2</span><spanclass="op">+</span> y<spanclass="op">*</span><spanclass="dv">2</span></span></code></pre></div>
202
213
<p>Simple. We could have also defined it as
203
214
<code>doubleUs x y = x + x + y + y</code>. Testing it out produces
204
215
pretty predictable results (remember to append this function to the
@@ -213,7 +224,8 @@ <h2 id="babys-first-functions">Baby’s first functions</h2>
213
224
<p>As expected, you can call your own functions from other functions
214
225
that you made. With that in mind, we could redefine
215
226
<code>doubleUs</code> like this:</p>
216
-
<preclass="haskell: hs"><code>doubleUs x y = doubleMe x + doubleMe y</code></pre>
227
+
<divclass="sourceCode" id="cb14"><pre
228
+
class="sourceCode haskell: hs"><codeclass="sourceCode haskell"><spanid="cb14-1"><ahref="#cb14-1" aria-hidden="true" tabindex="-1"></a>doubleUs x y <spanclass="ot">=</span> doubleMe x <spanclass="op">+</span> doubleMe y</span></code></pre></div>
217
229
<p>This is a very simple example of a common pattern you will see
218
230
throughout Haskell. Making basic functions that are obviously correct
219
231
and then combining them into more complex functions. This way you also
@@ -228,9 +240,10 @@ <h2 id="babys-first-functions">Baby’s first functions</h2>
228
240
<p>Now we’re going to make a function that multiplies a number by 2 but
229
241
only if that number is smaller than or equal to 100 because numbers
230
242
bigger than 100 are big enough as it is!</p>
231
-
<preclass="haskell: hs"><code>doubleSmallNumber x = if x > 100
232
-
then x
233
-
else x*2</code></pre>
243
+
<divclass="sourceCode" id="cb15"><pre
244
+
class="sourceCode haskell: hs"><codeclass="sourceCode haskell"><spanid="cb15-1"><ahref="#cb15-1" aria-hidden="true" tabindex="-1"></a>doubleSmallNumber x <spanclass="ot">=</span><spanclass="kw">if</span> x <spanclass="op">></span><spanclass="dv">100</span></span>
<p>Right here we introduced Haskell’s if statement. You’re probably
@@ -249,7 +262,8 @@ <h2 id="babys-first-functions">Baby’s first functions</h2>
249
262
that’s why it’s an expression. If we wanted to add one to every number
250
263
that’s produced in our previous function, we could have written its body
251
264
like this.</p>
252
-
<preclass="haskell: hs"><code>doubleSmallNumber' x = (if x > 100 then x else x*2) + 1</code></pre>
265
+
<divclass="sourceCode" id="cb16"><pre
266
+
class="sourceCode haskell: hs"><codeclass="sourceCode haskell"><spanid="cb16-1"><ahref="#cb16-1" aria-hidden="true" tabindex="-1"></a>doubleSmallNumber' x <spanclass="ot">=</span> (<spanclass="kw">if</span> x <spanclass="op">></span><spanclass="dv">100</span><spanclass="kw">then</span> x <spanclass="kw">else</span> x<spanclass="op">*</span><spanclass="dv">2</span>) <spanclass="op">+</span><spanclass="dv">1</span></span></code></pre></div>
253
267
<p>Had we omitted the parentheses, it would have added one only if
254
268
<code>x</code> wasn’t greater than 100. Note the <code>'</code> at the
255
269
end of the function name. That apostrophe doesn’t have any special
@@ -258,7 +272,8 @@ <h2 id="babys-first-functions">Baby’s first functions</h2>
258
272
a function (one that isn’t lazy) or a slightly modified version of a
259
273
function or a variable. Because <code>'</code> is a valid character in
<p><code>_</code> means that we don’t care what we’ll draw from the list
615
632
anyway so instead of writing a variable name that we’ll never use, we
616
633
just write <code>_</code>. This function replaces every element of a
@@ -620,7 +637,8 @@ <h2 id="im-a-list-comprehension">I’m a list comprehension</h2>
620
637
comprehensions to process and produce strings. Here’s a function that
621
638
takes a string and removes everything except uppercase letters from
622
639
it.</p>
623
-
<preclass="haskell: hs"><code>removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]</code></pre>
640
+
<divclass="sourceCode" id="cb52"><pre
641
+
class="sourceCode haskell: hs"><codeclass="sourceCode haskell"><spanid="cb52-1"><ahref="#cb52-1" aria-hidden="true" tabindex="-1"></a>removeNonUppercase st <spanclass="ot">=</span> [ c <spanclass="op">|</span> c <spanclass="ot"><-</span> st, c <spanclass="ot">`elem`</span> [<spanclass="ch">'A'</span><spanclass="op">..</span><spanclass="ch">'Z'</span>]]</span></code></pre></div>
0 commit comments