Skip to content

Commit 997b6a9

Browse files
memowesmith558
andauthored
Repair lines (learnyouahaskell#17)
* Remove DOS line endings (only present in some files) Command: dos2unix docs/*.html * Remove trailing whitespace Command: sed -i 's/[ \t]*$//' docs/*.html Co-authored-by: Stanislav (Stanley) Modrak <44023416+smith558@users.noreply.github.com>
1 parent 1c53468 commit 997b6a9

15 files changed

+9580
-9580
lines changed

docs/a-fistful-of-monads.html

Lines changed: 2099 additions & 2099 deletions
Large diffs are not rendered by default.

docs/chapters.html

Lines changed: 188 additions & 188 deletions
Large diffs are not rendered by default.

docs/for-a-few-monads-more.html

Lines changed: 3055 additions & 3055 deletions
Large diffs are not rendered by default.

docs/functors-applicative-functors-and-monoids.html

Lines changed: 2098 additions & 2098 deletions
Large diffs are not rendered by default.

docs/higher-order-functions.html

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ <h1 style="margin-left:-3px">Higher order functions</h1>
178178
[1,3,6,2,2]
179179
</pre>
180180
<p>You've probably noticed that each of these could be achieved with a list comprehension. <span class="fixed">map (+3) [1,5,3,1,6]</span> is the same as writing <span class="fixed">[x+3 | x &lt;- [1,5,3,1,6]]</span>. However, using <span class="fixed">map</span> is much more readable for cases where you only apply some function to the elements of a list, especially once you're dealing with maps of maps and then the whole thing with a lot of brackets can get a bit messy.</p>
181-
<p><span class="label function">filter</span> is a function that takes a predicate (a predicate is a function that tells whether something is true or not, so in our case, a function that returns a boolean value) and a list and then returns the list of elements that satisfy the predicate. The type signature and implementation go like this:</p>
181+
<p><span class="label function">filter</span> is a function that takes a predicate (a predicate is a function that tells whether something is true or not, so in our case, a function that returns a boolean value) and a list and then returns the list of elements that satisfy the predicate. The type signature and implementation go like this:</p>
182182
<pre name="code" class="haskell:hs">
183183
filter :: (a -&gt; Bool) -&gt; [a] -&gt; [a]
184184
filter _ [] = []
185-
filter p (x:xs)
185+
filter p (x:xs)
186186
| p x = x : filter p xs
187187
| otherwise = filter p xs
188188
</pre>
@@ -204,11 +204,11 @@ <h1 style="margin-left:-3px">Higher order functions</h1>
204204
<p>All of this could also be achived with list comprehensions by the use of predicates. There's no set rule for when to use <span class="fixed">map</span> and <span class="fixed">filter</span> versus using list comprehension, you just have to decide what's more readable depending on the code and the context. The <span class="fixed">filter</span> equivalent of applying several predicates in a list comprehension is either filtering something several times or joining the predicates with the logical <span class="fixed">&amp;&amp;</span> function. </p>
205205
<p>Remember our quicksort function from the <a href="recursion.html">previous chapter</a>? We used list comprehensions to filter out the list elements that are smaller than (or equal to) and larger than the pivot. We can achieve the same functionality in a more readable way by using <span class="fixed">filter</span>:</p>
206206
<pre name="code" class="haskell:ghci">
207-
quicksort :: (Ord a) =&gt; [a] -&gt; [a]
208-
quicksort [] = []
209-
quicksort (x:xs) =
207+
quicksort :: (Ord a) =&gt; [a] -&gt; [a]
208+
quicksort [] = []
209+
quicksort (x:xs) =
210210
let smallerSorted = quicksort (filter (&lt;=x) xs)
211-
biggerSorted = quicksort (filter (&gt;x) xs)
211+
biggerSorted = quicksort (filter (&gt;x) xs)
212212
in smallerSorted ++ [x] ++ biggerSorted
213213
</pre>
214214
<img src="https://s3.amazonaws.com/lyah/map.png" alt="map" class="left" width="210" height="115">
@@ -318,7 +318,7 @@ <h1 style="margin-left:-3px">Higher order functions</h1>
318318
</pre>
319319
<img src="https://s3.amazonaws.com/lyah/foldl.png" alt="foldl" class="left" width="172" height="348">
320320
<p>Let's take an in-depth look into how this fold happens. <span class="fixed">\acc x -&gt; acc + x</span> is the binary function. <span class="fixed">0</span> is the starting value and <span class="fixed">xs</span> is the list to be folded up. Now first, <span class="fixed">0</span> is used as the <span class="fixed">acc</span> parameter to the binary function and <span class="fixed">3</span> is used as the <span class="fixed">x</span> (or the current element) parameter. <span class="fixed">0 + 3</span> produces a <span class="fixed">3</span> and it becomes the new accumulator value, so to speak. Next up, <span class="fixed">3</span> is used as the accumulator value and <span class="fixed">5</span> as the current element and <span class="fixed">8</span> becomes the new accumulator value. Moving forward, <span class="fixed">8</span> is the accumulator value, <span class="fixed">2</span> is the current element, the new accumulator value is <span class="fixed">10</span>. Finally, that <span class="fixed">10</span> is used as the accumulator value and <span class="fixed">1</span> as the current element, producing an <span class="fixed">11</span>. Congratulations, you've done a fold! </p>
321-
<p>This professional diagram on the left illustrates how a fold happens, step by step (day by day!). The greenish brown number is the accumulator value. You can see how the list is sort of consumed up from the left side by the accumulator. Om nom nom nom! If we take into account that functions are curried, we can write this implementation ever more succinctly, like so:</p>
321+
<p>This professional diagram on the left illustrates how a fold happens, step by step (day by day!). The greenish brown number is the accumulator value. You can see how the list is sort of consumed up from the left side by the accumulator. Om nom nom nom! If we take into account that functions are curried, we can write this implementation ever more succinctly, like so:</p>
322322
<pre name="code" class="haskell:hs">
323323
sum' :: (Num a) =&gt; [a] -&gt; a
324324
sum' = foldl (+) 0
@@ -340,7 +340,7 @@ <h1 style="margin-left:-3px">Higher order functions</h1>
340340
<p>Of course, we could have implemented this function with a left fold too. It would be <span class="fixed">map' f xs = foldl (\acc x -&gt; acc ++ [f x]) [] xs</span>, but the thing is that the <span class="fixed">++</span> function is much more expensive than <span class="fixed">:</span>, so we usually use right folds when we're building up new lists from a list.</p>
341341
<img src="https://s3.amazonaws.com/lyah/washmachine.png" alt="fold this up!" class="right" width="250" height="205">
342342
<p>If you reverse a list, you can do a right fold on it just like you would have done a left fold and vice versa. Sometimes you don't even have to do that. The <span class="fixed">sum</span> function can be implemented pretty much the same with a left and right fold. One big difference is that right folds work on infinite lists, whereas left ones don't! To put it plainly, if you take an infinite list at some point and you fold it up from the right, you'll eventually reach the beginning of the list. However, if you take an infinite list at a point and you try to fold it up from the left, you'll never reach an end! </p>
343-
<p><em>Folds can be used to implement any function where you traverse a list once, element by element, and then return something based on that. Whenever you want to traverse a list to return something, chances are you want a fold.</em> That's why folds are, along with maps and filters, one of the most useful types of functions in functional programming.</p>
343+
<p><em>Folds can be used to implement any function where you traverse a list once, element by element, and then return something based on that. Whenever you want to traverse a list to return something, chances are you want a fold.</em> That's why folds are, along with maps and filters, one of the most useful types of functions in functional programming.</p>
344344
<p>The <span class="label function">foldl1</span> and <span class="label function">foldr1</span> functions work much like <span class="fixed">foldl</span> and <span class="fixed">foldr</span>, only you don't need to provide them with an explicit starting value. They assume the first (or last) element of the list to be the starting value and then start the fold with the element next to it. With that in mind, the <span class="fixed">sum</span> function can be implemented like so: <span class="fixed">sum = foldl1 (+)</span>. Because they depend on the lists they fold up having at least one element, they cause runtime errors if called with empty lists. <span class="fixed">foldl</span> and <span class="fixed">foldr</span>, on the other hand, work fine with empty lists. When making a fold, think about how it acts on an empty list. If the function doesn't make sense when given an empty list, you can probably use a <span class="fixed">foldl1</span> or <span class="fixed">foldr1</span> to implement it.</p>
345345
<p>Just to show you how powerful folds are, we're going to implement a bunch of standard library functions by using folds:</p>
346346
<pre name="code" class="haskell:hs">
@@ -437,8 +437,8 @@ <h1 style="margin-left:-3px">Higher order functions</h1>
437437
<p>But what about functions that take several parameters? Well, if we want to use them in function composition, we usually have to partially apply them just so much that each function takes just one parameter. <span class="fixed"> sum (replicate 5 (max 6.7 8.9))</span> can be rewritten as <span class="fixed">(sum . replicate 5 . max 6.7) 8.9</span> or as <span class="fixed">sum . replicate 5 . max 6.7 $ 8.9</span>. What goes on in here is this: a function that takes what <span class="fixed">max 6.7</span> takes and applies <span class="fixed">replicate 5</span> to it is created. Then, a function that takes the result of that and does a sum of it is created. Finally, that function is called with <span class="fixed">8.9</span>. But normally, you just read that as: apply <span class="fixed">8.9</span> to <span class="fixed">max 6.7</span>, then apply <span class="fixed">replicate 5</span> to that and then apply <span class="fixed">sum</span> to that. If you want to rewrite an expression with a lot of parentheses by using function composition, you can start by putting the last parameter of the innermost function after a <span class="fixed">$</span> and then just composing all the other function calls, writing them without their last parameter and putting dots between them. If you have <span class="fixed">replicate 100 (product (map (*3) (zipWith max [1,2,3,4,5] [4,5,6,7,8])))</span>, you can write it as <span class="fixed">replicate 100 . product . map (*3) . zipWith max [1,2,3,4,5] $ [4,5,6,7,8]</span>. If the expression ends with three parentheses, chances are that if you translate it into function composition, it'll have three composition operators.</p>
438438
<p>Another common use of function composition is defining functions in the so-called point free style (also called the point<i>less</i> style). Take for example this function that we wrote earlier:</p>
439439
<pre name="code" class="haskell:hs">
440-
sum' :: (Num a) =&gt; [a] -&gt; a
441-
sum' xs = foldl (+) 0 xs
440+
sum' :: (Num a) =&gt; [a] -&gt; a
441+
sum' xs = foldl (+) 0 xs
442442
</pre>
443443
<p>The <span class="fixed">xs</span> is exposed on both right sides. Because of currying, we can omit the <span class="fixed">xs</span> on both sides, because calling <span class="fixed">foldl (+) 0</span> creates a function that takes a list. Writing the function as <span class="fixed">sum' = foldl (+) 0</span> is called writing it in point free style. How would we write this in point free style?</p>
444444
<pre name="code" class="haskell:hs">
@@ -452,7 +452,7 @@ <h1 style="margin-left:-3px">Higher order functions</h1>
452452
<p>In the section about maps and filters, we solved a problem of finding the sum of all odd squares that are smaller than 10,000. Here's what the solution looks like when put into a function.</p>
453453
<pre name="code" class="haskell:hs">
454454
oddSquareSum :: Integer
455-
oddSquareSum = sum (takeWhile (&lt;10000) (filter odd (map (^2) [1..])))
455+
oddSquareSum = sum (takeWhile (&lt;10000) (filter odd (map (^2) [1..])))
456456
</pre>
457457
<p>Being such a fan of function composition, I would have probably written that like this:</p>
458458
<pre name="code" class="haskell:hs">
@@ -462,7 +462,7 @@ <h1 style="margin-left:-3px">Higher order functions</h1>
462462
<p>However, if there was a chance of someone else reading that code, I would have written it like this:</p>
463463
<pre name="code" class="haskell:hs">
464464
oddSquareSum :: Integer
465-
oddSquareSum =
465+
oddSquareSum =
466466
let oddSquares = filter odd $ map (^2) [1..]
467467
belowLimit = takeWhile (&lt;10000) oddSquares
468468
in sum belowLimit

docs/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<title>Learn You a Haskell for Great Good! (up-to-date)</title>
1616
<meta name="description"
17-
content='An up-to-date community maintained version of the renowned "Learn You a Haskell" (LYAH) guide for Haskell.
17+
content='An up-to-date community maintained version of the renowned "Learn You a Haskell" (LYAH) guide for Haskell.
1818
Packed with artwork, pop culture references, and most importantly, useful example code, this guide teaches functional fundamentals in a way you never thought possible.'>
1919
<meta name="keywords" content="computer science learn me a haskell learnyouahaskell haskell learning programming coding functional Learn You a Haskell for Great Good LYAH open source guide manual Miran Lipovaca wiki course teach">
2020
<link rel="canonical" href="https://learnyouahaskell.github.io/" />
@@ -88,7 +88,7 @@ <h1 style="display: none;">A guide to Haskell programming language</h1>
8888
<a id="read-button" href="chapters.html"
8989
style="display:block;text-indent:-9999px;position:absolute;width:167px;height:143px;top:477px;left:638px;">
9090
Read it online!</a>
91-
91+
9292
<!-- GitHub star button -->
9393
<div style="text-shadow: none;">
9494
<a class="github-button" href="https://github.com/learnyouahaskell/learnyouahaskell.github.io" data-show-count="true" aria-label="Star learnyouahaskell/learnyouahaskell.github.io on GitHub">Star</a>

0 commit comments

Comments
 (0)