<p><spanclass="sc"><abbrtitle="Too long; didn't read">tl;dr</abbr>: </span> A very short and dense tutorial for learning Haskell.</p>
<p>Thanks to:</p>
<ul>
<li><ahref="https://plus.google.com/u/0/113751420744109290534">Oleg Taykalo</a> you can find a Russian translation here: <ahref="http://habrahabr.ru/post/152889/">Part 1</a><em>&</em><ahref="http://habrahabr.ru/post/153383/">Part 2</a>,</li>
<li><ahref="http://silly-bytes.blogspot.fr">Daniel Campoverde</a> for the Spanish translation here: <ahref="http://silly-bytes.blogspot.fr/2016/06/aprende-haskell-rapido-y-dificil_29.html">Aprende Haskell rápido y difícil</a>,</li>
<li><ahref="http://github.com/joom">Joomy Korkut</a> for the Turkish translation here: <ahref="https://github.com/joom/zor-yoldan-haskell">Zor Yoldan Haskell</a>.</li>
<li><ahref="#maybe-monad">Maybe is a monad</a></li>
<li><ahref="#the-list-monad">The list monad</a></li>
</ul></li>
</ul></li>
<li><ahref="#appendix">Appendix</a>
<ul>
<li><ahref="#more-on-infinite-tree">More on Infinite Tree</a></li>
</ul></li>
</ul>
</div>
</blockquote>
</div>
<divclass="intro">
<p>I really believe all developers should learn Haskell. I don’t think everyone needs to be super Haskell ninjas, but they should at least discover what Haskell has to offer. Learning Haskell opens your mind.</p>
<p>Mainstream languages share the same foundations:</p>
<li>data structures, objects and classes (for most)</li>
</ul>
<p>Haskell is very different. The language uses a lot of concepts I had never heard about before. Many of those concepts will help you become a better programmer.</p>
<p>But learning Haskell can be hard. It was for me. In this article I try to provide what I lacked during my learning.</p>
<p>This article will certainly be hard to follow. This is on purpose. There is no shortcut to learning Haskell. It is hard and challenging. But I believe this is a good thing. It is because it is hard that Haskell is interesting.</p>
<p>The conventional method to learning Haskell is to read two books. First <ahref="http://learnyouahaskell.com">“Learn You a Haskell”</a> and just after <ahref="http://www.realworldhaskell.org">“Real World Haskell”</a>. I also believe this is the right way to go. But to learn what Haskell is all about, you’ll have to read them in detail.</p>
<p>In contrast, this article is a very brief and dense overview of all major aspects of Haskell. I also added some information I lacked while I learned Haskell.</p>
<p>The article contains five parts:</p>
<ul>
<li>Introduction: a short example to show Haskell can be friendly.</li>
<li>Basic Haskell: Haskell syntax, and some essential notions.</li>
<li>Hard Difficulty Part:
<ul>
<li>Functional style; a progressive example, from imperative to functional style</li>
<li>Types; types and a standard binary tree example</li>
<li>Infinite Structure; manipulate an infinite binary tree!</li>
</ul></li>
<li>Hell Difficulty Part:
<ul>
<li>Deal with IO; A very minimal example</li>
<li>IO trick explained; the hidden detail I lacked to understand IO</li>
<li>Monads; incredible how we can generalize</li>
</ul></li>
<li>Appendix:
<ul>
<li>More on infinite tree; a more math oriented discussion about infinite trees</li>
</ul></li>
</ul>
<blockquote>
Note: Each time you see a separator with a filename ending in <code>.lhs</code> you can click the filename to get this file. If you save the file as <code>filename.lhs</code>, you can run it with
<pre>
runhaskell filename.lhs
</pre>
<p>Some might not work, but most will. You should see a link just below.</p>
<p>There are different way to install Haskell, I would recommend to use <ahref="https://haskellstack.org"><code>stack</code></a>.</p>
<p>There are other way to install Haskell on your system you could visit, you can learn more about it by visiting <ahref="https://haskell.org">haskell.org</a> or <ahref="https://haskell-lang.org">haskell-lang.org</a></p>
<p>Tools:</p>
<ul>
<li><code>ghc</code>: Compiler similar to gcc for <code>C</code>.</li>
<p>Many books/articles about Haskell start by introducing some esoteric formula (quick sort, Fibonacci, etc…). I will do the exact opposite. At first I won’t show you any Haskell super power. I will start with similarities between Haskell and other programming languages. Let’s jump to the mandatory “Hello World”.</p>
<p>You could also download the literate Haskell source. You should see a link just above the introduction title. Download this file as <code>00_hello_world.lhs</code> and:</p>
<aclass="sourceLine"id="cb5-2"title="2"><spanclass="fu">print</span><spanclass="st">"What is your name?"</span></a>
<aclass="sourceLine"id="cb5-3"title="3"> name <spanclass="ot"><-</span><spanclass="fu">getLine</span></a>
<aclass="sourceLine"id="cb5-4"title="4"><spanclass="fu">print</span> (<spanclass="st">"Hello "</span><spanclass="op">++</span> name <spanclass="op">++</span><spanclass="st">"!"</span>)</a></code></pre></div>
</div>
<p>First, let us compare this with similar programs in a few imperative languages:</p>
<divclass="sourceCode"id="cb8"><preclass="sourceCode c"><codeclass="sourceCode c"><aclass="sourceLine"id="cb8-1"title="1"><spanclass="co">// In C</span></a>
<aclass="sourceLine"id="cb8-3"title="3"><spanclass="dt">int</span> main (<spanclass="dt">int</span> argc, <spanclass="dt">char</span> **argv) {</a>
<aclass="sourceLine"id="cb8-4"title="4"><spanclass="dt">char</span> name[<spanclass="dv">666</span>]; <spanclass="co">// <- An Evil Number!</span></a>
<aclass="sourceLine"id="cb8-5"title="5"><spanclass="co">// What if my name is more than 665 character long?</span></a>
<aclass="sourceLine"id="cb8-6"title="6"> printf(<spanclass="st">"What is your name?</span><spanclass="sc">\n</span><spanclass="st">"</span>);</a>
<p>The structure is the same, but there are some syntax differences. The main part of this tutorial will be dedicated to explaining why.</p>
<p>In Haskell there is a <code>main</code> function and every object has a type. The type of <code>main</code> is <code>IO ()</code>. This means <code>main</code> will cause side effects.</p>
<p>Just remember that Haskell can look a lot like mainstream imperative languages.</p>
<p>Before continuing you need to be warned about some essential properties of Haskell.</p>
<p><em>Functional</em></p>
<p>Haskell is a functional language. If you have an imperative language background, you’ll have to learn a lot of new things. Hopefully many of these new concepts will help you to program even in imperative languages.</p>
<p><em>Smart Static Typing</em></p>
<p>Instead of being in your way like in <code>C</code>, <code>C++</code> or <code>Java</code>, the type system is here to help you.</p>
<p><em>Purity</em></p>
<p>Generally your functions won’t modify anything in the outside world. This means they can’t modify the value of a variable, can’t get user input, can’t write on the screen, can’t launch a missile. On the other hand, parallelism will be very easy to achieve. Haskell makes it clear where effects occur and where your code is pure. Also, it will be far easier to reason about your program. Most bugs will be prevented in the pure parts of your program.</p>
<p>Furthermore, pure functions follow a fundamental law in Haskell:</p>
<blockquote>
<p>Applying a function with the same parameters always returns the same value.</p>
</blockquote>
<p><em>Laziness</em></p>
<p>Laziness by default is a very uncommon language design. By default, Haskell evaluates something only when it is needed. In consequence, it provides a very elegant way to manipulate infinite structures, for example.</p>
<p>A last warning about how you should read Haskell code. For me, it is like reading scientific papers. Some parts are very clear, but when you see a formula, just focus and read slower. Also, while learning Haskell, it <em>really</em> doesn’t matter much if you don’t understand syntax details. If you meet a <code>>>=</code>, <code><$></code>, <code><-</code> or any other weird symbol, just ignore them and follows the flow of the code.</p>
<h4id="function-declaration">
Function declaration
</h4>
<p>You might be used to declaring functions like this:</p>
<divclass="sourceCode"id="cb13"><preclass="sourceCode scheme"><codeclass="sourceCode scheme"><aclass="sourceLine"id="cb13-1"title="1">(<spanclass="ex">define</span><spanclass="fu"></span>(f x y)</a>
<aclass="sourceLine"id="cb13-2"title="2"> (<spanclass="op">+</span> (* x x) (* y y)))</a></code></pre></div>
<p>Finally, the Haskell way is:</p>
<divclass="sourceCode"id="cb14"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb14-1"title="1">f x y <spanclass="ot">=</span> x<spanclass="op">*</span>x <spanclass="op">+</span> y<spanclass="op">*</span>y</a></code></pre></div>
<p>Very clean. No parenthesis, no <code>def</code>.</p>
<p>Don’t forget, Haskell uses functions and types a lot. It is thus very easy to define them. The syntax was particularly well thought out for these objects.</p>
<h4id="a-type-example">
A Type Example
</h4>
<p>Although it is not mandatory, type information for functions is usually made explicit. It’s not mandatory because the compiler is smart enough to discover it for you. It’s a good idea because it indicates intent and understanding.</p>
<p>Let’s play a little. We declare the type using <code>::</code></p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb15"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb15-1"title="1"><spanclass="ot"> f ::</span><spanclass="dt">Int</span><spanclass="ot">-></span><spanclass="dt">Int</span><spanclass="ot">-></span><spanclass="dt">Int</span></a>
<aclass="sourceLine"id="cb15-2"title="2"> f x y <spanclass="ot">=</span> x<spanclass="op">*</span>x <spanclass="op">+</span> y<spanclass="op">*</span>y</a>
<aclass="sourceLine"id="cb15-3"title="3"></a>
<aclass="sourceLine"id="cb15-4"title="4"> main <spanclass="ot">=</span><spanclass="fu">print</span> (f <spanclass="dv">2</span><spanclass="dv">3</span>)</a></code></pre></div>
<aclass="sourceLine"id="cb17-2"title="2">f x y <spanclass="ot">=</span> x<spanclass="op">*</span>x <spanclass="op">+</span> y<spanclass="op">*</span>y</a>
<p>The solution: don’t declare a type for <code>f</code> for the moment and let Haskell infer the most general type for us:</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb19"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb19-1"title="1">f x y <spanclass="ot">=</span> x<spanclass="op">*</span>x <spanclass="op">+</span> y<spanclass="op">*</span>y</a>
<p>It works! Luckily, we don’t have to declare a new function for every single type. For example, in <code>C</code>, you’ll have to declare a function for <code>int</code>, for <code>float</code>, for <code>long</code>, for <code>double</code>, etc…</p>
<p>But, what type should we declare? To discover the type Haskell has found for us, just launch ghci:</p>
<pre><spanclass="low">
%</span> ghci<spanclass="low"><code>
GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude></code></span> let f x y = x*x + y*y
<spanclass="low"><code>Prelude></code></span> :type f
<code>f :: Num a => a -> a -> a</code>
</pre>
<p>Uh? What is this strange type?</p>
<pre><code>Num a => a -> a -> a</code></pre>
<p>First, let’s focus on the right part <code>a -> a -> a</code>. To understand it, just look at a list of progressive examples:</p>
<table>
<colgroup>
<colstyle="width: 27%"/>
<colstyle="width: 72%"/>
</colgroup>
<thead>
<trclass="header">
<th>Thewrittentype</th>
<thstyle="text-align: left;">Its meaning</th>
</tr>
</thead>
<tbody>
<trclass="odd">
<td><code>Int</code></td>
<tdstyle="text-align: left;">the type <code>Int</code></td>
</tr>
<trclass="even">
<td><code>Int -> Int</code></td>
<tdstyle="text-align: left;">the type function from <code>Int</code> to <code>Int</code></td>
</tr>
<trclass="odd">
<td><code>Float -> Int</code></td>
<tdstyle="text-align: left;">the type function from <code>Float</code> to <code>Int</code></td>
</tr>
<trclass="even">
<td><code>a -> Int</code></td>
<tdstyle="text-align: left;">the type function from any type to <code>Int</code></td>
</tr>
<trclass="odd">
<td><code>a -> a</code></td>
<tdstyle="text-align: left;">the type function from any type <code>a</code> to the same type <code>a</code></td>
</tr>
<trclass="even">
<td><code>a -> a -> a</code></td>
<tdstyle="text-align: left;">the type function of two arguments of any type <code>a</code> to the same type <code>a</code></td>
</tr>
</tbody>
</table>
<p>In the type <code>a -> a -> a</code>, the letter <code>a</code> is a <em>type variable</em>. It means <code>f</code> is a function with two arguments and both arguments and the result have the same type. The type variable <code>a</code> could take many different type values. For example <code>Int</code>, <code>Integer</code>, <code>Float</code>…</p>
<p>So instead of having a forced type like in <code>C</code> and having to declare a function for <code>int</code>, <code>long</code>, <code>float</code>, <code>double</code>, etc., we declare only one function like in a dynamically typed language.</p>
<p>This is sometimes called parametric polymorphism. It’s also called having your cake and eating it too.</p>
<p>Generally <code>a</code> can be any type, for example a <code>String</code> or an <code>Int</code>, but also more complex types, like <code>Trees</code>, other functions, etc. But here our type is prefixed with <code>Num a =></code>.</p>
<p><code>Num</code> is a <em>type class</em>. A type class can be understood as a set of types. <code>Num</code> contains only types which behave like numbers. More precisely, <code>Num</code> is class containing types which implement a specific list of functions, and in particular <code>(+)</code> and <code>(*)</code>.</p>
<p>Type classes are a very powerful language construct. We can do some incredibly powerful stuff with this. More on this later.</p>
<p>Finally, <code>Num a => a -> a -> a</code> means:</p>
<p>Let <code>a</code> be a type belonging to the <code>Num</code> type class. This is a function from type <code>a</code> to (<code>a -> a</code>).</p>
<p>Yes, strange. In fact, in Haskell no function really has two arguments. Instead all functions have only one argument. But we will note that taking two arguments is equivalent to taking one argument and returning a function taking the second argument as a parameter.</p>
<p>More precisely <code>f 3 4</code> is equivalent to <code>(f 3) 4</code>. Note <code>f 3</code> is a function:</p>
<pre><code>f :: Num a => a -> a -> a
g :: Num a => a -> a
g = f 3
g y ⇔ 3*3 + y*y</code></pre>
<p>Another notation exists for functions. The lambda notation allows us to create functions without assigning them a name. We call them anonymous functions. We could also have written:</p>
<pre><code>g = \y -> 3*3 + y*y</code></pre>
<p>The <code>\</code> is used because it looks like <code>λ</code> and is ASCII.</p>
<p>If you are not used to functional programming your brain should be starting to heat up. It is time to make a real application.</p>
<p>But just before that, we should verify the type system works as expected:</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb23"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb23-1"title="1"><spanclass="ot">f ::</span><spanclass="dt">Num</span> a <spanclass="ot">=></span> a <spanclass="ot">-></span> a <spanclass="ot">-></span> a</a>
<aclass="sourceLine"id="cb23-2"title="2">f x y <spanclass="ot">=</span> x<spanclass="op">*</span>x <spanclass="op">+</span> y<spanclass="op">*</span>y</a>
<p>It works, because, <code>3</code> is a valid representation both for Fractional numbers like Float and for Integer. As <code>2.4</code> is a Fractional number, <code>3</code> is then interpreted as being also a Fractional number.</p>
<p>If we force our function to work with different types, it will fail:</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb24"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb24-1"title="1"><spanclass="ot">f ::</span><spanclass="dt">Num</span> a <spanclass="ot">=></span> a <spanclass="ot">-></span> a <spanclass="ot">-></span> a</a>
<aclass="sourceLine"id="cb24-2"title="2">f x y <spanclass="ot">=</span> x<spanclass="op">*</span>x <spanclass="op">+</span> y<spanclass="op">*</span>y</a>
<aclass="sourceLine"id="cb24-8"title="8"><spanclass="co">-- won't work because type x ≠ type y</span></a>
<aclass="sourceLine"id="cb24-9"title="9">main <spanclass="ot">=</span><spanclass="fu">print</span> (f x y)</a></code></pre></div>
</div>
<p>The compiler complains. The two parameters must have the same type.</p>
<p>If you believe that this is a bad idea, and that the compiler should make the transformation from one type to another for you, you should really watch this great (and funny) video: <ahref="https://www.destroyallsoftware.com/talks/wat">WAT</a></p>
<p>I suggest that you skim this part. Think of it as a reference. Haskell has a lot of features. A lot of information is missing here. Come back here if the notation feels strange.</p>
<p>I use the <code>⇔</code> symbol to state that two expression are equivalent. It is a meta notation, <code>⇔</code> does not exists in Haskell. I will also use <code>⇒</code> to show what the return value of an expression is.</p>
<p><em>Remark</em>: In real code you shouldn’t use list of char to represent text. You should mostly use <code>Data.Text</code> instead. If you want to represent a stream of ASCII char, you should use <code>Data.ByteString</code>.</p>
</blockquote>
<h5id="tuples">
Tuples
</h5>
<p>The type of couple is <code>(a,b)</code>. Elements in a tuple can have different types.</p>
f :: a -> b -> c ⇔ f is a function from a to (b→c)
f :: (a -> b) -> c ⇔ f is a function from (a→b) to c</code></pre>
<p>Remember that defining the type of a function before its declaration isn’t mandatory. Haskell infers the most general type for you. But it is considered a good practice to do so.</p>
<p><em>Infix notation</em></p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb35"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb35-1"title="1"><spanclass="ot">square ::</span><spanclass="dt">Num</span> a <spanclass="ot">=></span> a <spanclass="ot">-></span> a</a>
<aclass="sourceLine"id="cb35-2"title="2">square x <spanclass="ot">=</span> x<spanclass="op">^</span><spanclass="dv">2</span></a></code></pre></div>
</div>
<p>Note <code>^</code> uses infix notation. For each infix operator there its associated prefix notation. You just have to put it inside parenthesis.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb36"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb36-1"title="1">square' x <spanclass="ot">=</span> (<spanclass="op">^</span>) x <spanclass="dv">2</span></a>
<aclass="sourceLine"id="cb36-2"title="2"></a>
<aclass="sourceLine"id="cb36-3"title="3">square'' x <spanclass="ot">=</span> (<spanclass="op">^</span><spanclass="dv">2</span>) x</a></code></pre></div>
</div>
<p>We can remove <code>x</code> in the left and right side! It’s called η-reduction.</p>
<p>An implementation of the absolute function.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb38"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb38-1"title="1"><spanclass="ot">absolute ::</span> (<spanclass="dt">Ord</span> a, <spanclass="dt">Num</span> a) <spanclass="ot">=></span> a <spanclass="ot">-></span> a</a>
<aclass="sourceLine"id="cb38-2"title="2">absolute x <spanclass="ot">=</span><spanclass="kw">if</span> x <spanclass="op">>=</span><spanclass="dv">0</span><spanclass="kw">then</span> x <spanclass="kw">else</span><spanclass="op">-</span>x</a></code></pre></div>
</div>
<p>Note: the <code>if .. then .. else</code> Haskell notation is more like the <code>¤?¤:¤</code> C operator. You cannot forget the <code>else</code>.</p>
<imgsrc="../../../../Scratch/img/blog/Haskell-the-Hard-Way/hr_giger_biomechanicallandscape_500.jpg"alt="Biomechanical Landscape by H.R. Giger"/>
</div>
<p>In this section, I will give a short example of the impressive refactoring ability provided by Haskell. We will select a problem and solve it in a standard imperative way. Then I will make the code evolve. The end result will be both more elegant and easier to adapt.</p>
<p>Let’s solve the following problem:</p>
<blockquote>
<p>Given a list of integers, return the sum of the even numbers in the list.</p>
<aclass="sourceLine"id="cb41-2"title="2"><spanclass="kw">var</span> result <spanclass="op">=</span><spanclass="dv">0</span><spanclass="op">;</span></a>
<p>In Haskell, by contrast, we don’t have variables or a for loop. One solution to achieve the same result without loops is to use recursion.</p>
<blockquote>
<p><em>Remark</em>: Recursion is generally perceived as slow in imperative languages. But this is generally not the case in functional programming. Most of the time Haskell will handle recursive functions efficiently.</p>
</blockquote>
<p>Here is a <code>C</code> version of the recursive function. Note that for simplicity I assume the int list ends with the first <code>0</code> value.</p>
<aclass="sourceLine"id="cb42-8"title="8"><spanclass="cf">if</span> (*list == <spanclass="dv">0</span>) { <spanclass="co">// if the list is empty</span></a>
<aclass="sourceLine"id="cb42-11"title="11"> x = list[<spanclass="dv">0</span>]; <spanclass="co">// let x be the first element of the list</span></a>
<aclass="sourceLine"id="cb42-12"title="12"> xs = list+<spanclass="dv">1</span>; <spanclass="co">// let xs be the list without x</span></a>
<aclass="sourceLine"id="cb42-13"title="13"><spanclass="cf">if</span> ( <spanclass="dv">0</span> == (x%<spanclass="dv">2</span>) ) { <spanclass="co">// if x is even</span></a>
<p>Keep this code in mind. We will translate it into Haskell. First, however, I need to introduce three simple but useful functions we will use:</p>
<divclass="sourceCode"id="cb43"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb43-1"title="1"><spanclass="fu">even</span><spanclass="ot"> ::</span><spanclass="dt">Integral</span> a <spanclass="ot">=></span> a <spanclass="ot">-></span><spanclass="dt">Bool</span></a>
<p><code>even</code> verifies if a number is even.</p>
<divclass="sourceCode"id="cb44"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb44-1"title="1"><spanclass="fu">even</span><spanclass="ot"> ::</span><spanclass="dt">Integral</span> a <spanclass="ot">=></span> a <spanclass="ot">-></span><spanclass="dt">Bool</span></a>
<p>The first Haskell solution. The function <code>evenSum</code> returns the sum of all even numbers in a list:</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb47"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb47-1"title="1"><spanclass="co">-- Version 1</span></a>
<aclass="sourceLine"id="cb47-8"title="8"><spanclass="kw">else</span><spanclass="kw">let</span> x <spanclass="ot">=</span><spanclass="fu">head</span> l</a>
<p>Next, we can use sub functions using <code>where</code> or <code>let</code>. This way our <code>accumSum</code> function won’t pollute the namespace of our module.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb50"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb50-1"title="1"><spanclass="co">-- Version 2</span></a>
<aclass="sourceLine"id="cb50-2"title="2"><spanclass="ot">evenSum ::</span><spanclass="dt">Integral</span> a <spanclass="ot">=></span> [a] <spanclass="ot">-></span> a</a>
<aclass="sourceLine"id="cb50-3"title="3"></a>
<aclass="sourceLine"id="cb50-4"title="4">evenSum l <spanclass="ot">=</span> accumSum <spanclass="dv">0</span> l</a>
<aclass="sourceLine"id="cb50-5"title="5"><spanclass="kw">where</span> accumSum n l <spanclass="ot">=</span></a>
<aclass="sourceLine"id="cb50-6"title="6"><spanclass="kw">if</span> l <spanclass="op">==</span> []</a>
<aclass="sourceLine"id="cb50-8"title="8"><spanclass="kw">else</span><spanclass="kw">let</span> x <spanclass="ot">=</span><spanclass="fu">head</span> l</a>
<divclass="sourceCode"id="cb52"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb52-1"title="1"><spanclass="co">-- Version 3</span></a>
<aclass="sourceLine"id="cb52-2"title="2">evenSum l <spanclass="ot">=</span> accumSum <spanclass="dv">0</span> l</a>
<aclass="sourceLine"id="cb53-2"title="2">foo l <spanclass="ot">=</span><spanclass="op"><</span>y<spanclass="op">></span></a></code></pre></div>
<p>But pattern matching goes even further. It is also able to inspect the inner data of a complex value. We can replace</p>
<divclass="sourceCode"id="cb54"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb54-1"title="1">foo l <spanclass="ot">=</span><spanclass="kw">let</span> x <spanclass="ot">=</span><spanclass="fu">head</span> l</a>
<p>In Haskell you can simplify function definitions by η-reducing them. For example, instead of writing:</p>
<divclass="sourceCode"id="cb57"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb57-1"title="1">f x <spanclass="ot">=</span> (some expresion) x</a></code></pre></div>
<p>you can simply write</p>
<divclass="sourceCode"id="cb58"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb58-1"title="1">f <spanclass="ot">=</span> some expression</a></code></pre></div>
<p>We use this method to remove the <code>l</code>:</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb59"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb59-1"title="1"><spanclass="co">-- Version 4</span></a>
<aclass="sourceLine"id="cb59-2"title="2"><spanclass="ot">evenSum ::</span><spanclass="dt">Integral</span> a <spanclass="ot">=></span> [a] <spanclass="ot">-></span> a</a>
<p>To make things even better we should use higher order functions. What are these beasts? Higher order functions are functions taking functions as parameters.</p>
<p>Here are some examples:</p>
<divclass="sourceCode"id="cb61"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb61-1"title="1"><spanclass="fu">filter</span><spanclass="ot"> ::</span> (a <spanclass="ot">-></span><spanclass="dt">Bool</span>) <spanclass="ot">-></span> [a] <spanclass="ot">-></span> [a]</a>
<aclass="sourceLine"id="cb61-2"title="2"><spanclass="fu">map</span><spanclass="ot"> ::</span> (a <spanclass="ot">-></span> b) <spanclass="ot">-></span> [a] <spanclass="ot">-></span> [b]</a>
<aclass="sourceLine"id="cb61-3"title="3"><spanclass="fu">foldl</span><spanclass="ot"> ::</span> (a <spanclass="ot">-></span> b <spanclass="ot">-></span> a) <spanclass="ot">-></span> a <spanclass="ot">-></span> [b] <spanclass="ot">-></span> a</a></code></pre></div>
<p>Let’s proceed by small steps.</p>
<divclass="sourceCode"id="cb62"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb62-1"title="1"><spanclass="co">-- Version 5</span></a>
<aclass="sourceLine"id="cb62-2"title="2">evenSum l <spanclass="ot">=</span> mysum <spanclass="dv">0</span> (<spanclass="fu">filter</span><spanclass="fu">even</span> l)</a>
<p>The function <code>filter</code> takes a function of type (<code>a -> Bool</code>) and a list of type <code>[a]</code>. It returns a list containing only elements for which the function returned <code>true</code>.</p>
<p>Our next step is to use another technique to accomplish the same thing as a loop. We will use the <code>foldl</code> function to accumulate a value as we pass through the list. The function <code>foldl</code> captures a general coding pattern:</p>
<pre>
myfunc list = foo <spanclass="blue">initialValue</span><spanclass="green">list</span>
myfunc list = foldl <spanclass="yellow">bar</span><spanclass="blue">initialValue</span><spanclass="green">list</span>
</pre>
<p>If you really want to know how the magic works, here is the definition of <code>foldl</code>:</p>
<divclass="sourceCode"id="cb64"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb64-1"title="1"><spanclass="fu">foldl</span> f z [] <spanclass="ot">=</span> z</a>
<aclass="sourceLine"id="cb64-2"title="2"><spanclass="fu">foldl</span> f z (x<spanclass="op">:</span>xs) <spanclass="ot">=</span><spanclass="fu">foldl</span> f (f z x) xs</a></code></pre></div>
<divclass="sourceCode"id="cb65"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb65-1"title="1"><spanclass="fu">foldl</span> f z [x1,<spanclass="op">...</span>xn]</a>
<aclass="sourceLine"id="cb65-2"title="2">⇔ f (<spanclass="op">...</span> (f (f z x1) x2) <spanclass="op">...</span>) xn</a></code></pre></div>
<p>But as Haskell is lazy, it doesn’t evaluate <code>(f z x)</code> and simply pushes it onto the stack. This is why we generally use <code>foldl'</code> instead of <code>foldl</code>; <code>foldl'</code> is a <em>strict</em> version of <code>foldl</code>. If you don’t understand what lazy and strict means, don’t worry, just follow the code as if <code>foldl</code> and <code>foldl'</code> were identical.</p>
<p>Now our new version of <code>evenSum</code> becomes:</p>
<divclass="sourceCode"id="cb66"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb66-1"title="1"><spanclass="co">-- Version 6</span></a>
<aclass="sourceLine"id="cb66-2"title="2"><spanclass="co">-- foldl' isn't accessible by default</span></a>
<aclass="sourceLine"id="cb66-3"title="3"><spanclass="co">-- we need to import it from the module Data.List</span></a>
<aclass="sourceLine"id="cb66-5"title="5">evenSum l <spanclass="ot">=</span> foldl' mysum <spanclass="dv">0</span> (<spanclass="fu">filter</span><spanclass="fu">even</span> l)</a>
<aclass="sourceLine"id="cb66-6"title="6"><spanclass="kw">where</span> mysum acc value <spanclass="ot">=</span> acc <spanclass="op">+</span> value</a></code></pre></div>
<p>We can also simplify this by using directly a lambda notation. This way we don’t have to create the temporary name <code>mysum</code>.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb67"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb67-1"title="1"><spanclass="co">-- Version 7</span></a>
<aclass="sourceLine"id="cb67-2"title="2"><spanclass="co">-- Generally it is considered a good practice</span></a>
<aclass="sourceLine"id="cb67-3"title="3"><spanclass="co">-- to import only the necessary function(s)</span></a>
<divclass="sourceCode"id="cb70"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb70-1"title="1"><spanclass="co">-- Version 8</span></a>
<p>Another useful higher order function is <code>(.)</code>. The <code>(.)</code> function corresponds to mathematical composition.</p>
<divclass="sourceCode"id="cb71"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb71-1"title="1">(f <spanclass="op">.</span> g <spanclass="op">.</span> h) x ⇔ f ( g (h x))</a></code></pre></div>
<p>We can take advantage of this operator to η-reduce our function:</p>
<divclass="sourceCode"id="cb72"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb72-1"title="1"><spanclass="co">-- Version 9</span></a>
<p>Also, we could rename some parts to make it clearer:</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb73"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb73-1"title="1"><spanclass="co">-- Version 10</span></a>
<p>It is time to discuss the direction our code has moved as we introduced more functional idioms. What did we gain by using higher order functions?</p>
<p>At first, you might think the main difference is terseness. But in fact, it has more to do with better thinking. Suppose we want to modify our function slightly, for example, to get the sum of all even squares of elements of the list.</p>
<p>The <code>map</code> function simply applies a function to all the elements of a list.</p>
<p>We didn’t have to modify anything <em>inside</em> the function definition. This makes the code more modular. But in addition you can think more mathematically about your function. You can also use your function interchangably with others, as needed. That is, you can compose, map, fold, filter using your new function.</p>
<p>Modifying version 1 is left as an exercise to the reader ☺.</p>
<p>If you believe we have reached the end of generalization, then know you are very wrong. For example, there is a way to not only use this function on lists but on any recursive type. If you want to know how, I suggest you to read this quite fun article: <ahref="http://eprints.eemcs.utwente.nl/7281/01/db-utwente-40501F46.pdf">Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire by Meijer, Fokkinga and Paterson</a>.</p>
<p>This example should show you how great pure functional programming is. Unfortunately, using pure functional programming isn’t well suited to all usages. Or at least such a language hasn’t been found yet.</p>
<p>One of the great powers of Haskell is the ability to create DSLs (Domain Specific Language) making it easy to change the programming paradigm.</p>
<p>In fact, Haskell is also great when you want to write imperative style programming. Understanding this was really hard for me to grasp when first learning Haskell. A lot of effort tends to go into explaining the superiority of the functional approach. Then when you start using an imperative style with Haskell, it can be hard to understand when and how to use it.</p>
<p>But before talking about this Haskell super-power, we must talk about another essential aspect of Haskell: <em>Types</em>.</p>
<li><code>type Name = AnotherType</code> is just an alias and the compiler doesn’t mark any difference between <code>Name</code> and <code>AnotherType</code>.</li>
<li><code>data Name = NameConstructor AnotherType</code> does mark a difference.</li>
<li><code>data</code> can construct structures which can be recursives.</li>
<li><code>deriving</code> is magic and creates functions for you.</li>
</ul>
</blockquote>
<p>In Haskell, types are strong and static.</p>
<p>Why is this important? It will help you <em>greatly</em> to avoid mistakes. In Haskell, most bugs are caught during the compilation of your program. And the main reason is because of the type inference during compilation. Type inference makes it easy to detect where you used the wrong parameter at the wrong place, for example.</p>
<h4id="type-inference">
Type inference
</h4>
<p>Static typing is generally essential for fast execution. But most statically typed languages are bad at generalizing concepts. Haskell’s saving grace is that it can <em>infer</em> types.</p>
<p>Here is a simple example, the <code>square</code> function in Haskell:</p>
<divclass="sourceCode"id="cb78"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb78-1"title="1">square x <spanclass="ot">=</span> x <spanclass="op">*</span> x</a></code></pre></div>
<p>This function can <code>square</code> any Numeral type. You can provide <code>square</code> with an <code>Int</code>, an <code>Integer</code>, a <code>Float</code> a <code>Fractional</code> and even <code>Complex</code>. Proof by example:</p>
<pre><code>% ghci
GHCi, version 7.0.4:
...
Prelude> let square x = x*x
Prelude> square 2
4
Prelude> square 2.1
4.41
Prelude> -- load the Data.Complex module
Prelude> :m Data.Complex
Prelude Data.Complex> square (2 :+ 1)
3.0 :+ 4.0</code></pre>
<p><code>x :+ y</code> is the notation for the complex (<i>x + iy</i>).</p>
<p>Now compare with the amount of code necessary in C:</p>
<p>For each type, you need to write a new function. The only way to work around this problem is to use some meta-programming trick, for example using the pre-processor. In C++ there is a better way, C++ templates:</p>
<p>C++ does a far better job than C in this regard. But for more complex functions the syntax can be hard to follow: see <ahref="http://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/">this article</a> for example.</p>
<p>In C++ you must declare that a function can work with different types. In Haskell, the opposite is the case. The function will be as general as possible by default.</p>
<p>Type inference gives Haskell the feeling of freedom that dynamically typed languages provide. But unlike dynamically typed languages, most errors are caught before run time. Generally, in Haskell:</p>
<blockquote>
<p>“if it compiles it certainly does what you intended”</p>
<aclass="sourceLine"id="cb82-5"title="5">showInfos name color <spanclass="ot">=</span><spanclass="st">"Name: "</span><spanclass="op">++</span> name</a>
<aclass="sourceLine"id="cb82-11"title="11">main <spanclass="ot">=</span><spanclass="fu">putStrLn</span><spanclass="op">$</span> showInfos name color</a></code></pre></div>
<aclass="sourceLine"id="cb84-10"title="10">main <spanclass="ot">=</span><spanclass="fu">putStrLn</span><spanclass="op">$</span> showInfos name color</a></code></pre></div>
</div>
<p>Now if you switch parameters of <code>showInfos</code>, the compiler complains! So this is a potential mistake you will never make again and the only price is to be more verbose.</p>
<p>Also notice that constructors are functions:</p>
<p>Generally the usage is to use the same name for the DataTypeName and DataTypeConstructor.</p>
<p>Example:</p>
<divclass="sourceCode"id="cb87"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb87-1"title="1"><spanclass="kw">data</span><spanclass="dt">Complex</span> a <spanclass="ot">=</span><spanclass="dt">Num</span> a <spanclass="ot">=></span><spanclass="dt">Complex</span> a a</a></code></pre></div>
<p>And many accessors are made for you. Furthermore you can use another order when setting values.</p>
<p>Example:</p>
<divclass="sourceCode"id="cb89"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb89-1"title="1"><spanclass="kw">data</span><spanclass="dt">Complex</span> a <spanclass="ot">=</span><spanclass="dt">Num</span> a <spanclass="ot">=></span><spanclass="dt">Complex</span> {<spanclass="ot"> real ::</span> a,<spanclass="ot"> img ::</span> a}</a>
<p>You already encountered a recursive type: lists. You can re-create lists, but with a more verbose syntax:</p>
<divclass="sourceCode"id="cb90"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb90-1"title="1"><spanclass="kw">data</span><spanclass="dt">List</span> a <spanclass="ot">=</span><spanclass="dt">Empty</span><spanclass="op">|</span><spanclass="dt">Cons</span> a (<spanclass="dt">List</span> a)</a></code></pre></div>
<p>If you really want to use an easier syntax you can use an infix name for constructors.</p>
<aclass="sourceLine"id="cb91-2"title="2"><spanclass="kw">data</span><spanclass="dt">List</span> a <spanclass="ot">=</span><spanclass="dt">Nil</span><spanclass="op">|</span> a <spanclass="op">:::</span> (<spanclass="dt">List</span> a)</a></code></pre></div>
<p>The number after <code>infixr</code> gives the precedence.</p>
<p>If you want to be able to print (<code>Show</code>), read (<code>Read</code>), test equality (<code>Eq</code>) and compare (<code>Ord</code>) your new data structure you can tell Haskell to derive the appropriate functions for you.</p>
<aclass="sourceLine"id="cb92-2"title="2"><spanclass="kw">data</span><spanclass="dt">List</span> a <spanclass="ot">=</span><spanclass="dt">Nil</span><spanclass="op">|</span> a <spanclass="op">:::</span> (<spanclass="dt">List</span> a)</a>
<p>When you add <code>deriving (Show)</code> to your data declaration, Haskell creates a <code>show</code> function for you. We’ll see soon how you can use your own <code>show</code> function.</p>
<aclass="sourceLine"id="cb96-3"title="3"><spanclass="kw">data</span><spanclass="dt">BinTree</span> a <spanclass="ot">=</span><spanclass="dt">Empty</span></a>
<aclass="sourceLine"id="cb96-4"title="4"><spanclass="op">|</span><spanclass="dt">Node</span> a (<spanclass="dt">BinTree</span> a) (<spanclass="dt">BinTree</span> a)</a>
<p>Just for fun, let’s code a better display for our trees. I simply had fun making a nice function to display trees in a general way. You can safely skip this part if you find it too difficult to follow.</p>
<p>We have a few changes to make. We remove the <code>deriving (Show)</code> from the declaration of our <code>BinTree</code> type. And it might also be useful to make our BinTree an instance of (<code>Eq</code> and <code>Ord</code>) so we will be able to test equality and compare trees.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb100"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb100-1"title="1"><spanclass="kw">data</span><spanclass="dt">BinTree</span> a <spanclass="ot">=</span><spanclass="dt">Empty</span></a>
<aclass="sourceLine"id="cb100-2"title="2"><spanclass="op">|</span><spanclass="dt">Node</span> a (<spanclass="dt">BinTree</span> a) (<spanclass="dt">BinTree</span> a)</a>
<p>Without the <code>deriving (Show)</code>, Haskell doesn’t create a <code>show</code> method for us. We will create our own version of <code>show</code>. To achieve this, we must declare that our newly created type <code>BinTree a</code> is an instance of the type class <code>Show</code>. The general syntax is:</p>
<divclass="sourceCode"id="cb101"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb101-1"title="1"><spanclass="kw">instance</span><spanclass="dt">Show</span> (<spanclass="dt">BinTree</span> a) <spanclass="kw">where</span></a>
<aclass="sourceLine"id="cb101-2"title="2"><spanclass="fu">show</span> t <spanclass="ot">=</span><spanclass="op">...</span><spanclass="co">-- You declare your function here</span></a></code></pre></div>
<p>Here is my version of how to show a binary tree. Don’t worry about the apparent complexity. I made a lot of improvements in order to display even stranger objects.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb102"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb102-1"title="1"><spanclass="co">-- declare BinTree a to be an instance of Show</span></a>
<aclass="sourceLine"id="cb102-2"title="2"><spanclass="kw">instance</span> (<spanclass="dt">Show</span> a) <spanclass="ot">=></span><spanclass="dt">Show</span> (<spanclass="dt">BinTree</span> a) <spanclass="kw">where</span></a>
<aclass="sourceLine"id="cb102-3"title="3"><spanclass="co">-- will start by a '<' before the root</span></a>
<aclass="sourceLine"id="cb102-4"title="4"><spanclass="co">-- and put a : a begining of line</span></a>
<aclass="sourceLine"id="cb102-5"title="5"><spanclass="fu">show</span> t <spanclass="ot">=</span><spanclass="st">"<"</span><spanclass="op">++</span> replace <spanclass="ch">'\n'</span><spanclass="st">"\n: "</span> (treeshow <spanclass="st">""</span> t)</a>
<aclass="sourceLine"id="cb102-15"title="15"><spanclass="co">-- Right branch is empty</span></a>
<aclass="sourceLine"id="cb102-16"title="16"> treeshow pref (<spanclass="dt">Node</span> x left <spanclass="dt">Empty</span>) <spanclass="ot">=</span></a>
<p>Now it is far better! The root is shown by starting the line with the <code><</code> character. And each following line starts with a <code>:</code>. But we could also use another type.</p>
<p>As we can test equality and order trees, we can make tree of trees!</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb108"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb108-1"title="1"><spanclass="fu">putStrLn</span><spanclass="st">"\nBinary tree of Char binary trees:"</span></a>
<divclass="sourceCode"id="cb110"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb110-1"title="1"><spanclass="fu">putStrLn</span><spanclass="st">"\nTree of Binary trees of Char binary trees:"</span></a>
<pre><code>Binary tree of Binary trees of Char binary trees:
<<< 'Y'
: : : `--'O'
: : `--< 'D'
: : : |--'A'
: : : `--'W'
: : : `--'G'
: |--<< 'I'
: | : `--< 'H'
: | : : |--'E'
: | : : | `--'A'
: | : : | `--'D'
: | : : `--'R'
: `--<< 'Y'
: : : `--'O'
: : : `--'U'
: : `--< 'L'
: : : `--'I'
: : : |--'E'
: : : `--'K'
: : `--< 'T'
: : : `--'R'
: : : |--'E'
: : : `--'S'</code></pre>
<p>Notice how duplicate trees aren’t inserted; there is only one tree corresponding to <code>"I","HEARD"</code>. We have this for (almost) free, because we have declared Tree to be an instance of <code>Eq</code>.</p>
<p>See how awesome this structure is: We can make trees containing not only integers, strings and chars, but also other trees. And we can even make a tree containing a tree of trees!</p>
<p>It is often said that Haskell is <em>lazy</em>.</p>
<p>In fact, if you are a bit pedantic, you should say that <ahref="http://www.haskell.org/haskellwiki/Lazy_vs._non-strict">Haskell is <em>non-strict</em></a>. Laziness is just a common implementation for non-strict languages.</p>
<p>Then what does “not-strict” mean? From the Haskell wiki:</p>
<blockquote>
<p>Reduction (the mathematical term for evaluation) proceeds from the outside in.</p>
<p>so if you have <code>(a+(b*c))</code> then you first reduce <code>+</code> first, then you reduce the inner <code>(b*c)</code></p>
<aclass="sourceLine"id="cb115-3"title="3"><spanclass="kw">data</span><spanclass="dt">BinTree</span> a <spanclass="ot">=</span><spanclass="dt">Empty</span></a>
<aclass="sourceLine"id="cb115-4"title="4"><spanclass="op">|</span><spanclass="dt">Node</span> a (<spanclass="dt">BinTree</span> a) (<spanclass="dt">BinTree</span> a)</a>
<divclass="sourceCode"id="cb116"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb116-1"title="1"><spanclass="co">-- declare BinTree a to be an instance of Show</span></a>
<aclass="sourceLine"id="cb116-2"title="2"><spanclass="kw">instance</span> (<spanclass="dt">Show</span> a) <spanclass="ot">=></span><spanclass="dt">Show</span> (<spanclass="dt">BinTree</span> a) <spanclass="kw">where</span></a>
<aclass="sourceLine"id="cb116-3"title="3"><spanclass="co">-- will start by a '<' before the root</span></a>
<aclass="sourceLine"id="cb116-4"title="4"><spanclass="co">-- and put a : a begining of line</span></a>
<aclass="sourceLine"id="cb116-5"title="5"><spanclass="fu">show</span> t <spanclass="ot">=</span><spanclass="st">"<"</span><spanclass="op">++</span> replace <spanclass="ch">'\n'</span><spanclass="st">"\n: "</span> (treeshow <spanclass="st">""</span> t)</a>
<aclass="sourceLine"id="cb116-11"title="11"> treeshow pref (<spanclass="dt">Node</span> x left <spanclass="dt">Empty</span>) <spanclass="ot">=</span></a>
<p>A complete binary tree where each node is equal to 0. Now I will prove you can manipulate this object using the following function:</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb118"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb118-1"title="1"><spanclass="co">-- take all element of a BinTree</span></a>
<aclass="sourceLine"id="cb118-2"title="2"><spanclass="co">-- up to some depth</span></a>
<aclass="sourceLine"id="cb118-5"title="5">treeTakeDepth n (<spanclass="dt">Node</span> x left right) <spanclass="ot">=</span><spanclass="kw">let</span></a>
<aclass="sourceLine"id="cb121-3"title="3"> dec (<spanclass="dt">Node</span> x l r) <spanclass="ot">=</span><spanclass="dt">Node</span> (x<spanclass="op">-</span><spanclass="dv">1</span>) (dec l) (dec r)</a>
<aclass="sourceLine"id="cb121-4"title="4"> inc (<spanclass="dt">Node</span> x l r) <spanclass="ot">=</span><spanclass="dt">Node</span> (x<spanclass="op">+</span><spanclass="dv">1</span>) (inc l) (inc r)</a></code></pre></div>
</div>
<p>Another way to create this tree is to use a higher order function. This function should be similar to <code>map</code>, but should work on <code>BinTree</code> instead of list. Here is such a function:</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb122"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb122-1"title="1"><spanclass="co">-- apply a function to each node of Tree</span></a>
<aclass="sourceLine"id="cb122-2"title="2"><spanclass="ot">treeMap ::</span> (a <spanclass="ot">-></span> b) <spanclass="ot">-></span><spanclass="dt">BinTree</span> a <spanclass="ot">-></span><spanclass="dt">BinTree</span> b</a>
<aclass="sourceLine"id="cb122-3"title="3">treeMap f <spanclass="dt">Empty</span><spanclass="ot">=</span><spanclass="dt">Empty</span></a>
<aclass="sourceLine"id="cb122-4"title="4">treeMap f (<spanclass="dt">Node</span> x left right) <spanclass="ot">=</span><spanclass="dt">Node</span> (f x)</a>
<aclass="sourceLine"id="cb122-5"title="5"> (treeMap f left)</a>
<aclass="sourceLine"id="cb122-6"title="6"> (treeMap f right)</a></code></pre></div>
</div>
<p><em>Hint</em>: I won’t talk more about this here. If you are interested in the generalization of <code>map</code> to other data structures, search for functor and <code>fmap</code>.</p>
<p>Congratulations for getting so far! Now, some of the really hardcore stuff can start.</p>
<p>If you are like me, you should get the functional style. You should also understand a bit more the advantages of laziness by default. But you also don’t really understand where to start in order to make a real program. And in particular:</p>
<ul>
<li>How do you deal with effects?</li>
<li>Why is there a strange imperative-like notation for dealing with IO?</li>
</ul>
<p>Be prepared, the answers might be complex. But they are all very rewarding.</p>
<p>A typical function doing <code>IO</code> looks a lot like an imperative program:</p>
<pre><code>f :: IO a
f = do
x <- action1
action2 x
y <- action3
action4 x y</code></pre>
<ul>
<li>To set a value to an object we use <code><-</code> .</li>
<li>The type of each line is <code>IO *</code>; in this example:
<ul>
<li><code>action1 :: IO b</code></li>
<li><code>action2 x :: IO ()</code></li>
<li><code>action3 :: IO c</code></li>
<li><code>action4 x y :: IO a</code></li>
<li><code>x :: b</code>, <code>y :: c</code></li>
</ul></li>
<li>Few objects have the type <code>IO a</code>, this should help you choose. In particular you cannot use pure functions directly here. To use pure functions you could do <code>action2 (purefunction x)</code> for example.</li>
</ul>
</blockquote>
<p>In this section, I will explain how to use IO, not how it works. You’ll see how Haskell separates the pure from the impure parts of the program.</p>
<p>Don’t stop because you’re trying to understand the details of the syntax. Answers will come in the next section.</p>
<p>What to achieve?</p>
<blockquote>
<p>Ask a user to enter a list of numbers. Print the sum of the numbers</p>
<aclass="sourceLine"id="cb128-5"title="5"><spanclass="fu">putStrLn</span><spanclass="st">"Enter a list of numbers (separated by comma):"</span></a>
<p>We should also pay attention to the effect of the <code><-</code> symbol.</p>
<pre><code>do
x <- something</code></pre>
<p>If <code>something :: IO a</code> then <code>x :: a</code>.</p>
<p>Another important note about using <code>IO</code>: All lines in a do block must be of one of the two forms:</p>
<pre><code>action1 :: IO a
-- in this case, generally a = ()</code></pre>
<p>ou</p>
<pre><code>value <- action2 -- where
-- action2 :: IO b
-- value :: b</code></pre>
<p>These two kinds of line will correspond to two different ways of sequencing actions. The meaning of this sentence should be clearer by the end of the next section.</p>
<p>Argh! An evil error message and a crash! Our first improvement will simply be to answer with a more friendly message.</p>
<p>In order to do this, we must detect that something went wrong. Here is one way to do this: use the type <code>Maybe</code>. This is a very common type in Haskell.</p>
<p>What is this thing? <code>Maybe</code> is a type which takes one parameter. Its definition is:</p>
<divclass="sourceCode"id="cb135"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb135-1"title="1"><spanclass="kw">data</span><spanclass="dt">Maybe</span> a <spanclass="ot">=</span><spanclass="dt">Nothing</span><spanclass="op">|</span><spanclass="dt">Just</span> a</a></code></pre></div>
<p>This is a nice way to tell there was an error while trying to create/compute a value. The <code>maybeRead</code> function is a great example of this. This is a function similar to the function <code>read</code><ahref="#fn4"class="footnote-ref"id="fnref4"><sup>4</sup></a>, but if something goes wrong the returned value is <code>Nothing</code>. If the value is right, it returns <code>Just <the value></code>. Don’t try to understand too much of this function. I use a lower level function than <code>read</code>: <code>reads</code>.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb136"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb136-1"title="1"><spanclass="ot">maybeRead ::</span><spanclass="dt">Read</span> a <spanclass="ot">=></span><spanclass="dt">String</span><spanclass="ot">-></span><spanclass="dt">Maybe</span> a</a>
<aclass="sourceLine"id="cb136-2"title="2">maybeRead s <spanclass="ot">=</span><spanclass="kw">case</span><spanclass="fu">reads</span> s <spanclass="kw">of</span></a>
<p>Now to be a bit more readable, we define a function which goes like this: If the string has the wrong format, it will return <code>Nothing</code>. Otherwise, for example for “1,2,3”, it will return <code>Just [1,2,3]</code>.</p>
<aclass="sourceLine"id="cb138-3"title="3"><spanclass="fu">putStrLn</span><spanclass="st">"Enter a list of numbers (separated by comma):"</span></a>
<aclass="sourceLine"id="cb138-7"title="7"><spanclass="dt">Just</span> l <spanclass="ot">-></span><spanclass="fu">print</span> (<spanclass="fu">sum</span> l)</a>
<aclass="sourceLine"id="cb138-8"title="8"><spanclass="dt">Nothing</span><spanclass="ot">-></span><spanclass="fu">error</span><spanclass="st">"Bad format. Good Bye."</span></a></code></pre></div>
</div>
<p>In case of error, we display a nice error message.</p>
<p>Note that the type of each expression in the main’s <code>do</code> block remains of the form <code>IO a</code>. The only strange construction is <code>error</code>. I’ll just say here that <code>error msg</code> takes the needed type (here <code>IO ()</code>).</p>
<p>One very important thing to note is the type of all the functions defined so far. There is only one function which contains <code>IO</code> in its type: <code>main</code>. This means main is impure. But main uses <code>getListFromString</code> which is pure. So it’s clear just by looking at declared types which functions are pure and which are impure.</p>
<p>Why does purity matter? Among the many advantages, here are three:</p>
<ul>
<li>It is far easier to think about pure code than impure code.</li>
<li>Purity protects you from all the hard-to-reproduce bugs that are due to side effects.</li>
<li>You can evaluate pure functions in any order or in parallel without risk.</li>
</ul>
<p>This is why you should generally put as most code as possible inside pure functions.</p>
<aclass="sourceLine"id="cb139-3"title="3"><spanclass="ot">maybeRead ::</span><spanclass="dt">Read</span> a <spanclass="ot">=></span><spanclass="dt">String</span><spanclass="ot">-></span><spanclass="dt">Maybe</span> a</a>
<aclass="sourceLine"id="cb139-4"title="4">maybeRead s <spanclass="ot">=</span><spanclass="kw">case</span><spanclass="fu">reads</span> s <spanclass="kw">of</span></a>
<aclass="sourceLine"id="cb140-3"title="3"><spanclass="fu">putStrLn</span><spanclass="st">"Enter a list of numbers (separated by comma):"</span></a>
<p>This function is of type <code>IO [Integer]</code>. Such a type means that we retrieved a value of type <code>[Integer]</code> through some IO actions. Some people might explain while waving their hands:</p>
<blockquote>
<p>«This is an <code>[Integer]</code> inside an <code>IO</code>»</p>
</blockquote>
<p>If you want to understand the details behind all of this, you’ll have to read the next section. But really, if you just want to <em>use</em> IO just practice a little and remember to think about the type.</p>
<p>We have finished with our introduction to <code>IO</code>. This was quite fast. Here are the main things to remember:</p>
<ul>
<li>in the <code>do</code> block, each expression must have the type <code>IO a</code>. You are then limited with regard to the range of expressions available. For example, <code>getLine</code>, <code>print</code>, <code>putStrLn</code>, etc…</li>
<li>Try to externalize the pure functions as much as possible.</li>
<li>the <code>IO a</code> type means: an IO <em>action</em> which returns an element of type <code>a</code>. <code>IO</code> represents actions; under the hood, <code>IO a</code> is the type of a function. Read the next section if you are curious.</li>
</ul>
<p>If you practice a bit, you should be able to <em>use</em><code>IO</code>.</p>
<blockquote>
<p><em>Exercises</em>:</p>
<ul>
<li>Make a program that sums all of its arguments. Hint: use the function <code>getArgs</code>.</li>
<aclass="sourceLine"id="cb146-3"title="3"><spanclass="fu">putStrLn</span><spanclass="st">"Enter a list of numbers (separated by commas):"</span></a>
<p>First remark: this looks imperative. Haskell is powerful enough to make impure code look imperative. For example, if you wish you could create a <code>while</code> in Haskell. In fact, for dealing with <code>IO</code>, an imperative style is generally more appropriate.</p>
<p>But you should have noticed that the notation is a bit unusual. Here is why, in detail.</p>
<p>In an impure language, the state of the world can be seen as a huge hidden global variable. This hidden variable is accessible by all functions of your language. For example, you can read and write a file in any function. Whether a file exists or not is a difference in the possible states that the world can take.</p>
<p>In Haskell the current state of the world is not hidden. Rather, it is <em>explicitly</em> said that <code>main</code> is a function that <em>potentially</em> changes the state of the world. Its type is then something like:</p>
<p>Not all functions may access this variable. Those which have access to this variable are impure. Functions to which the world variable isn’t provided are pure<ahref="#fn5"class="footnote-ref"id="fnref5"><sup>5</sup></a>.</p>
<p>Haskell considers the state of the world as an input variable to <code>main</code>. But the real type of main is closer to this one<ahref="#fn6"class="footnote-ref"id="fnref6"><sup>6</sup></a>:</p>
<p>where <code>a</code> is the type of the result. For example, a <code>getChar</code> function should have the type <code>World -> (Char, World)</code>.</p>
<p>Another thing to note is the trick to fix the order of evaluation. In Haskell, in order to evaluate <code>f a b</code>, you have many choices:</p>
<ul>
<li>first eval <code>a</code> then <code>b</code> then <code>f a b</code></li>
<li>first eval <code>b</code> then <code>a</code> then <code>f a b</code>.</li>
<li>eval <code>a</code> and <code>b</code> in parallel then <code>f a b</code></li>
</ul>
<p>This is true because we’re working in a pure part of the language.</p>
<p>Now, if you look at the main function, it is clear you must eval the first line before the second one since to evaluate the second line you have to get a parameter given by the evaluation of the first line.</p>
<p>This trick works like a charm. The compiler will at each step provide a pointer to a new real world id. Under the hood, <code>print</code> will evaluate as:</p>
<ul>
<li>print something on the screen</li>
<li>modify the id of the world</li>
<li>evaluate as <code>((),new world id)</code>.</li>
</ul>
<p>Now, if you look at the style of the main function, it is clearly awkward. Let’s try to do the same to the <code>askUser</code> function:</p>
<aclass="sourceLine"id="cb153-2"title="2"><spanclass="kw">let</span> (_,w1) <spanclass="ot">=</span><spanclass="fu">putStrLn</span><spanclass="st">"Enter a list of numbers:"</span><spanclass="kw">in</span></a>
<p>This is similar, but awkward. Look at all these temporary <code>w?</code> names.</p>
<p>The lesson is: naive IO implementation in Pure functional languages is awkward!</p>
<p>Fortunately, there is a better way to handle this problem. We see a pattern. Each line is of the form:</p>
<divclass="sourceCode"id="cb154"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb154-1"title="1"><spanclass="kw">let</span> (y,w') <spanclass="ot">=</span> action x w <spanclass="kw">in</span></a></code></pre></div>
<p>Even if for some lines the first <code>x</code> argument isn’t needed. The output type is a couple, <code>(answer, newWorldValue)</code>. Each function <code>f</code> must have a type similar to:</p>
<p>Now, we will do a magic trick. We will make the temporary world symbols “disappear”. We will <code>bind</code> the two lines. Let’s define the <code>bind</code> function. Its type is quite intimidating at first:</p>
<aclass="sourceLine"id="cb160-2"title="2"><spanclass="ot">-></span> (a <spanclass="ot">-></span> (<spanclass="dt">World</span><spanclass="ot">-></span> (b,<spanclass="dt">World</span>)))</a>
<aclass="sourceLine"id="cb162-2"title="2"><spanclass="fu">print</span><spanclass="ot"> ::</span><spanclass="dt">Show</span> a <spanclass="ot">=></span> a <spanclass="ot">-></span><spanclass="dt">IO</span> ()</a></code></pre></div>
<p><code>getLine</code> is an IO action which takes world as a parameter and returns a couple <code>(String, World)</code>. This can be summarized as: <code>getLine</code> is of type <code>IO String</code>, which we also see as an IO action which will return a String “embeded inside an IO”.</p>
<p>The function <code>print</code> is also interesting. It takes one argument which can be shown. In fact it takes two arguments. The first is the value to print and the other is the state of world. It then returns a couple of type <code>((), World)</code>. This means that it changes the state of the world, but doesn’t yield any more data.</p>
<p>This new <code>IO a</code> type helps us simplify the type of <code>bind</code>:</p>
<p>As print is of type <code>Show a => a -> (World -> ((), World))</code>, we know <code>res = ()</code> (<code>unit</code> type). If you didn’t see what was magic here, let’s try with three lines this time.</p>
<p>Didn’t you notice something? Yes, no temporary World variables are used anywhere! This is <em>MA</em>. <em>GIC</em>.</p>
<p>We can use a better notation. Let’s use <code>(>>=)</code> instead of <code>bind</code>. <code>(>>=)</code> is an infix function like <code>(+)</code>; reminder <code>3 + 4 ⇔ (+) 3 4</code></p>
<p>Note that you can use <code>x</code> in <code>action2</code> and <code>x</code> and <code>y</code> in <code>action3</code>.</p>
<p>But what about the lines not using the <code><-</code>? Easy, another function <code>blindBind</code>:</p>
<divclass="sourceCode"id="cb174"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb174-1"title="1"><spanclass="ot">blindBind ::</span><spanclass="dt">IO</span> a <spanclass="ot">-></span><spanclass="dt">IO</span> b <spanclass="ot">-></span><spanclass="dt">IO</span> b</a>
<p>I didn’t simplify this definition for the purposes of clarity. Of course, we can use a better notation: we’ll use the <code>(>>)</code> operator.</p>
<divclass="sourceCode"id="cb177"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb177-1"title="1"><spanclass="ot">putInIO ::</span> a <spanclass="ot">-></span><spanclass="dt">IO</span> a</a>
<aclass="sourceLine"id="cb177-2"title="2">putInIO x <spanclass="ot">=</span><spanclass="dt">IO</span> (\w <spanclass="ot">-></span> (x,w))</a></code></pre></div>
<p>This is the general way to put pure values inside the “IO context”. The general name for <code>putInIO</code> is <code>return</code>. This is quite a bad name when you learn Haskell. <code>return</code> is very different from what you might be used to.</p>
<aclass="sourceLine"id="cb178-4"title="4"><spanclass="fu">putStrLn</span><spanclass="st">"Enter a list of numbers (separated by commas):"</span></a>
<aclass="sourceLine"id="cb179-3"title="3"><spanclass="ot">maybeRead ::</span><spanclass="dt">Read</span> a <spanclass="ot">=></span><spanclass="dt">String</span><spanclass="ot">-></span><spanclass="dt">Maybe</span> a</a>
<aclass="sourceLine"id="cb179-4"title="4">maybeRead s <spanclass="ot">=</span><spanclass="kw">case</span><spanclass="fu">reads</span> s <spanclass="kw">of</span></a>
<aclass="sourceLine"id="cb179-11"title="11"><spanclass="fu">putStrLn</span><spanclass="st">"Enter a list of numbers (sep. by commas):"</span><spanclass="op">>></span></a>
<imgsrc="../../../../Scratch/img/blog/Haskell-the-Hard-Way/dali_reve.jpg"alt="Dali, reve. It represents a weapon out of the mouth of a tiger, itself out of the mouth of another tiger, itself out of the mouth of a fish itself out of a grenade. I could have choosen a picture of the Human centipede as it is a very good representation of what a monad really is. But just to think about it, I find this disgusting and that wasn"/>
</div>
<p>Now the secret can be revealed: <code>IO</code> is a <em>monad</em>. Being a monad means you have access to some syntactical sugar with the <code>do</code> notation. But mainly, you have access to a coding pattern which will ease the flow of your code.</p>
<blockquote>
<p><strong>Important remarks</strong>:</p>
<ul>
<li>Monad are not necessarily about effects! There are a lot of <em>pure</em> monads.</li>
<li>Monad are more about sequencing</li>
</ul>
</blockquote>
<p>In Haskell, <code>Monad</code> is a type class. To be an instance of this type class, you must provide the functions <code>(>>=)</code> and <code>return</code>. The function <code>(>>)</code> is derived from <code>(>>=)</code>. Here is how the type class <code>Monad</code> is declared (basically):</p>
<divclass="sourceCode"id="cb180"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb180-1"title="1"><spanclass="kw">class</span><spanclass="dt">Monad</span> m <spanclass="kw">where</span></a>
<aclass="sourceLine"id="cb180-2"title="2"><spanclass="ot"> (>>=) ::</span> m a <spanclass="ot">-></span> (a <spanclass="ot">-></span> m b) <spanclass="ot">-></span> m b</a>
<aclass="sourceLine"id="cb180-3"title="3"><spanclass="ot"> return ::</span> a <spanclass="ot">-></span> m a</a>
<aclass="sourceLine"id="cb180-4"title="4"></a>
<aclass="sourceLine"id="cb180-5"title="5"><spanclass="ot"> (>>) ::</span> m a <spanclass="ot">-></span> m b <spanclass="ot">-></span> m b</a>
<aclass="sourceLine"id="cb180-6"title="6"> f <spanclass="op">>></span> g <spanclass="ot">=</span> f <spanclass="op">>>=</span> \_ <spanclass="ot">-></span> g</a>
<aclass="sourceLine"id="cb180-7"title="7"></a>
<aclass="sourceLine"id="cb180-8"title="8"><spanclass="co">-- You should generally safely ignore this function</span></a>
<aclass="sourceLine"id="cb180-9"title="9"><spanclass="co">-- which I believe exists for historical reasons</span></a>
<aclass="sourceLine"id="cb180-10"title="10"><spanclass="ot"> fail ::</span><spanclass="dt">String</span><spanclass="ot">-></span> m a</a>
<li>the keyword <code>class</code> is not your friend. A Haskell class is <em>not</em> a class of the kind you will find in object-oriented programming. A Haskell class has a lot of similarities with Java interfaces. A better word would have been <code>typeclass</code>, since that means a set of types. For a type to belong to a class, all functions of the class must be provided for this type.</li>
<li>In this particular example of type class, the type <code>m</code> must be a type that takes an argument. for example <code>IO a</code>, but also <code>Maybe a</code>, <code>[a]</code>, etc…</li>
<li>To be a useful monad, your function must obey some rules. If your construction does not obey these rules strange things might happens:</li>
</ul>
</blockquote>
<blockquote>
<pre><code>return a >>= k == k a
m >>= return == m
m >>= (\x -> k x >>= h) == (m >>= k) >>= h</code></pre>
</blockquote>
<h4id="maybe-monad">
Maybe is a monad
</h4>
<p>There are a lot of different types that are instances of <code>Monad</code>. One of the easiest to describe is <code>Maybe</code>. If you have a sequence of <code>Maybe</code> values, you can use monads to manipulate them. It is particularly useful to remove very deep <code>if..then..else..</code> constructions.</p>
<p>Imagine a complex bank operation. You are eligible to gain about 700€ only if you can afford to follow a list of operations without your balance dipping below zero.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb182"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb182-1"title="1">deposit value account <spanclass="ot">=</span> account <spanclass="op">+</span> value</a>
<aclass="sourceLine"id="cb182-2"title="2">withdraw value account <spanclass="ot">=</span> account <spanclass="op">-</span> value</a>
<aclass="sourceLine"id="cb182-3"title="3"></a>
<aclass="sourceLine"id="cb182-4"title="4"><spanclass="ot">eligible ::</span> (<spanclass="dt">Num</span> a,<spanclass="dt">Ord</span> a) <spanclass="ot">=></span> a <spanclass="ot">-></span><spanclass="dt">Bool</span></a>
<p>Now, let’s make it better using Maybe and the fact that it is a Monad</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb183"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb183-1"title="1"><spanclass="ot">deposit ::</span> (<spanclass="dt">Num</span> a) <spanclass="ot">=></span> a <spanclass="ot">-></span> a <spanclass="ot">-></span><spanclass="dt">Maybe</span> a</a>
<aclass="sourceLine"id="cb183-2"title="2">deposit value account <spanclass="ot">=</span><spanclass="dt">Just</span> (account <spanclass="op">+</span> value)</a>
<aclass="sourceLine"id="cb183-3"title="3"></a>
<aclass="sourceLine"id="cb183-4"title="4"><spanclass="ot">withdraw ::</span> (<spanclass="dt">Num</span> a,<spanclass="dt">Ord</span> a) <spanclass="ot">=></span> a <spanclass="ot">-></span> a <spanclass="ot">-></span><spanclass="dt">Maybe</span> a</a>
<aclass="sourceLine"id="cb183-5"title="5">withdraw value account <spanclass="ot">=</span><spanclass="kw">if</span> (account <spanclass="op"><</span> value) </a>
<aclass="sourceLine"id="cb183-9"title="9"><spanclass="ot">eligible ::</span> (<spanclass="dt">Num</span> a, <spanclass="dt">Ord</span> a) <spanclass="ot">=></span> a <spanclass="ot">-></span><spanclass="dt">Maybe</span><spanclass="dt">Bool</span></a>
<aclass="sourceLine"id="cb183-19"title="19"><spanclass="fu">print</span><spanclass="op">$</span> eligible <spanclass="dv">300</span><spanclass="co">-- Just True</span></a>
<divclass="sourceCode"id="cb184"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb184-1"title="1"><spanclass="ot">deposit ::</span> (<spanclass="dt">Num</span> a) <spanclass="ot">=></span> a <spanclass="ot">-></span> a <spanclass="ot">-></span><spanclass="dt">Maybe</span> a</a>
<aclass="sourceLine"id="cb184-2"title="2">deposit value account <spanclass="ot">=</span><spanclass="dt">Just</span> (account <spanclass="op">+</span> value)</a>
<aclass="sourceLine"id="cb184-3"title="3"></a>
<aclass="sourceLine"id="cb184-4"title="4"><spanclass="ot">withdraw ::</span> (<spanclass="dt">Num</span> a,<spanclass="dt">Ord</span> a) <spanclass="ot">=></span> a <spanclass="ot">-></span> a <spanclass="ot">-></span><spanclass="dt">Maybe</span> a</a>
<aclass="sourceLine"id="cb184-5"title="5">withdraw value account <spanclass="ot">=</span><spanclass="kw">if</span> (account <spanclass="op"><</span> value) </a>
<aclass="sourceLine"id="cb184-9"title="9"><spanclass="ot">eligible ::</span> (<spanclass="dt">Num</span> a, <spanclass="dt">Ord</span> a) <spanclass="ot">=></span> a <spanclass="ot">-></span><spanclass="dt">Maybe</span><spanclass="dt">Bool</span></a>
<aclass="sourceLine"id="cb184-19"title="19"><spanclass="fu">print</span><spanclass="op">$</span> eligible <spanclass="dv">300</span><spanclass="co">-- Just True</span></a>
<p>We have proven that Monads are a good way to make our code more elegant. Note this idea of code organization, in particular for <code>Maybe</code> can be used in most imperative languages. In fact, this is the kind of construction we make naturally.</p>
<blockquote>
<p>An important remark:</p>
<p>The first element in the sequence being evaluated to <code>Nothing</code> will stop the complete evaluation. This means you don’t execute all lines. You get this for free, thanks to laziness.</p>
</blockquote>
<p>You could also replay these example with the definition of <code>(>>=)</code> for <code>Maybe</code> in mind:</p>
<aclass="sourceLine"id="cb185-2"title="2"><spanclass="ot"> (>>=) ::</span><spanclass="dt">Maybe</span> a <spanclass="ot">-></span> (a <spanclass="ot">-></span><spanclass="dt">Maybe</span> b) <spanclass="ot">-></span><spanclass="dt">Maybe</span> b</a>
<aclass="sourceLine"id="cb185-4"title="4"> (<spanclass="dt">Just</span> x) <spanclass="op">>>=</span> f <spanclass="ot">=</span> f x</a>
<aclass="sourceLine"id="cb185-5"title="5"></a>
<aclass="sourceLine"id="cb185-6"title="6"><spanclass="fu">return</span> x <spanclass="ot">=</span><spanclass="dt">Just</span> x</a></code></pre></div>
<p>The <code>Maybe</code> monad proved to be useful while being a very simple example. We saw the utility of the <code>IO</code> monad. But now for a cooler example, lists.</p>
<aclass="sourceLine"id="cb188-2"title="2"> y <spanclass="ot"><-</span> allCases,</a>
<aclass="sourceLine"id="cb188-3"title="3"> z <spanclass="ot"><-</span> allCases,</a>
<aclass="sourceLine"id="cb188-4"title="4"><spanclass="dv">4</span><spanclass="op">*</span>x <spanclass="op">+</span><spanclass="dv">2</span><spanclass="op">*</span>y <spanclass="op"><</span> z ]</a></code></pre></div>
</div>
<p>I won’t list all the monads, since there are many of them. Using monads simplifies the manipulation of several notions in pure languages. In particular, monads are very useful for:</p>
<ul>
<li>IO,</li>
<li>non-deterministic computation,</li>
<li>generating pseudo random numbers,</li>
<li>keeping configuration state,</li>
<li>writing state,</li>
<li>…</li>
</ul>
<p>If you have followed me until here, then you’ve done it! You know monads<ahref="#fn7"class="footnote-ref"id="fnref7"><sup>7</sup></a>!</p>
<p>In the section <ahref="#infinite-structures">Infinite Structures</a> we saw some simple constructions. Unfortunately we removed two properties from our tree:</p>
<oltype="1">
<li>no duplicate node value</li>
<li>well ordered tree</li>
</ol>
<p>In this section we will try to keep the first property. Concerning the second one, we must relax it but we’ll discuss how to keep it as much as possible.</p>
<divstyle="display:none">
<p>This code is mostly the same as the one in the <ahref="#trees">tree section</a>.</p>
<aclass="sourceLine"id="cb189-2"title="2"><spanclass="kw">data</span><spanclass="dt">BinTree</span> a <spanclass="ot">=</span><spanclass="dt">Empty</span></a>
<aclass="sourceLine"id="cb189-3"title="3"><spanclass="op">|</span><spanclass="dt">Node</span> a (<spanclass="dt">BinTree</span> a) (<spanclass="dt">BinTree</span> a) </a>
<aclass="sourceLine"id="cb189-6"title="6"><spanclass="co">-- declare BinTree a to be an instance of Show</span></a>
<aclass="sourceLine"id="cb189-7"title="7"><spanclass="kw">instance</span> (<spanclass="dt">Show</span> a) <spanclass="ot">=></span><spanclass="dt">Show</span> (<spanclass="dt">BinTree</span> a) <spanclass="kw">where</span></a>
<aclass="sourceLine"id="cb189-8"title="8"><spanclass="co">-- will start by a '<' before the root</span></a>
<aclass="sourceLine"id="cb189-9"title="9"><spanclass="co">-- and put a : a begining of line</span></a>
<aclass="sourceLine"id="cb189-10"title="10"><spanclass="fu">show</span> t <spanclass="ot">=</span><spanclass="st">"<"</span><spanclass="op">++</span> replace <spanclass="ch">'\n'</span><spanclass="st">"\n: "</span> (treeshow <spanclass="st">""</span> t)</a>
<aclass="sourceLine"id="cb189-16"title="16"> treeshow pref (<spanclass="dt">Node</span> x left <spanclass="dt">Empty</span>) <spanclass="ot">=</span></a>
<aclass="sourceLine"id="cb192-3"title="3">treeTakeDepth n (<spanclass="dt">Node</span> x left right) <spanclass="ot">=</span><spanclass="kw">let</span></a>
<p>will loop forever. Simply because it will try to access the head of <code>filter (<1) [2..]</code>. But <code>filter</code> is not smart enought to understand that the result is the empty list.</p>
<p>Nonetheless, it is still a very cool example of what non strict programs have to offer.</p>
<p>Left as an exercise to the reader:</p>
<ul>
<li>Prove the existence of a number <code>n</code> so that <code>treeTakeDepth n (treeFromList shuffle)</code> will enter an infinite loop.</li>
<li>Find an upper bound for <code>n</code>.</li>
<li>Prove there is no <code>shuffle</code> list so that, for any depth, the program ends.</li>
<aclass="sourceLine"id="cb196-3"title="3"><spanclass="kw">data</span><spanclass="dt">BinTree</span> a <spanclass="ot">=</span><spanclass="dt">Empty</span></a>
<aclass="sourceLine"id="cb196-4"title="4"><spanclass="op">|</span><spanclass="dt">Node</span> a (<spanclass="dt">BinTree</span> a) (<spanclass="dt">BinTree</span> a) </a>
<divclass="sourceCode"id="cb197"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb197-1"title="1"><spanclass="co">-- declare BinTree a to be an instance of Show</span></a>
<aclass="sourceLine"id="cb197-2"title="2"><spanclass="kw">instance</span> (<spanclass="dt">Show</span> a) <spanclass="ot">=></span><spanclass="dt">Show</span> (<spanclass="dt">BinTree</span> a) <spanclass="kw">where</span></a>
<aclass="sourceLine"id="cb197-3"title="3"><spanclass="co">-- will start by a '<' before the root</span></a>
<aclass="sourceLine"id="cb197-4"title="4"><spanclass="co">-- and put a : a begining of line</span></a>
<aclass="sourceLine"id="cb197-5"title="5"><spanclass="fu">show</span> t <spanclass="ot">=</span><spanclass="st">"<"</span><spanclass="op">++</span> replace <spanclass="ch">'\n'</span><spanclass="st">"\n: "</span> (treeshow <spanclass="st">""</span> t)</a>
<aclass="sourceLine"id="cb197-11"title="11"> treeshow pref (<spanclass="dt">Node</span> x left <spanclass="dt">Empty</span>) <spanclass="ot">=</span></a>
<aclass="sourceLine"id="cb197-41"title="41">treeTakeDepth n (<spanclass="dt">Node</span> x left right) <spanclass="ot">=</span><spanclass="kw">let</span></a>
<aclass="sourceLine"id="cb197-45"title="45"><spanclass="dt">Node</span> x nl nr</a></code></pre></div>
</div>
</div>
<p>In order to resolve these problem we will modify slightly our <code>treeFromList</code> and <code>shuffle</code> function.</p>
<p>A first problem, is the lack of infinite different number in our implementation of <code>shuffle</code>. We generated only <code>4331</code> different numbers. To resolve this we make a slightly better <code>shuffle</code> function.</p>
<aclass="sourceLine"id="cb198-4"title="4"> p x <spanclass="ot">=</span> m<spanclass="op">*</span>x<spanclass="op">^</span><spanclass="dv">2</span><spanclass="op">+</span> n<spanclass="op">*</span>x <spanclass="op">+</span> o <spanclass="co">-- some polynome</span></a>
<aclass="sourceLine"id="cb198-5"title="5"> m <spanclass="ot">=</span><spanclass="dv">3123</span></a>
<aclass="sourceLine"id="cb198-6"title="6"> n <spanclass="ot">=</span><spanclass="dv">31</span></a>
<aclass="sourceLine"id="cb198-7"title="7"> o <spanclass="ot">=</span><spanclass="dv">7641</span></a>
<aclass="sourceLine"id="cb198-8"title="8"> c <spanclass="ot">=</span><spanclass="dv">1237</span></a></code></pre></div>
</div>
<p>This shuffle function has the property (hopefully) not to have an upper nor lower bound. But having a better shuffle list isn’t enough not to enter an infinite loop.</p>
<p>Generally, we cannot decide whether <code>filter (<x) xs</code> is empty. Then to resolve this problem, I’ll authorize some error in the creation of our binary tree. This new version of code can create binary tree which don’t have the following property for some of its nodes:</p>
<blockquote>
<p>Any element of the left (resp. right) branch must all be strictly inferior (resp. superior) to the label of the root.</p>
</blockquote>
<p>Remark it will remains <em>mostly</em> an ordered binary tree. Furthermore, by construction, each node value is unique in the tree.</p>
<p>Here is our new version of <code>treeFromList</code>. We simply have replaced <code>filter</code> by <code>safefilter</code>.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb199"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb199-1"title="1"><spanclass="ot">treeFromList ::</span> (<spanclass="dt">Ord</span> a, <spanclass="dt">Show</span> a) <spanclass="ot">=></span> [a] <spanclass="ot">-></span><spanclass="dt">BinTree</span> a</a>
<aclass="sourceLine"id="cb199-3"title="3">treeFromList (x<spanclass="op">:</span>xs) <spanclass="ot">=</span><spanclass="dt">Node</span> x left right</a>
<aclass="sourceLine"id="cb199-5"title="5"> left <spanclass="ot">=</span> treeFromList <spanclass="op">$</span> safefilter (<spanclass="op"><</span>x) xs</a>
<aclass="sourceLine"id="cb199-6"title="6"> right <spanclass="ot">=</span> treeFromList <spanclass="op">$</span> safefilter (<spanclass="op">></span>x) xs</a></code></pre></div>
</div>
<p>This new function <code>safefilter</code> is almost equivalent to <code>filter</code> but don’t enter infinite loop if the result is a finite list. If it cannot find an element for which the test is true after 10000 consecutive steps, then it considers to be the end of the search.</p>
<divclass="codehighlight">
<divclass="sourceCode"id="cb200"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb200-1"title="1"><spanclass="ot">safefilter ::</span> (a <spanclass="ot">-></span><spanclass="dt">Bool</span>) <spanclass="ot">-></span> [a] <spanclass="ot">-></span> [a]</a>
<aclass="sourceLine"id="cb200-2"title="2">safefilter f l <spanclass="ot">=</span> safefilter' f l nbTry</a>
<p>You should realize the time to print each value is different. This is because Haskell compute each value when it needs it. And in this case, this is when asked to print it on the screen.</p>
<p>Impressively enough, try to replace the depth from <code>8</code> to <code>100</code>. It will work without killing your RAM! The flow and the memory management is done naturally by Haskell.</p>
<p>Left as an exercise to the reader:</p>
<ul>
<li>Even with large constant value for <code>deep</code> and <code>nbTry</code>, it seems to work nicely. But in the worst case, it can be exponential. Create a worst case list to give as parameter to <code>treeFromList</code>.<br/>
<em>hint</em>: think about (<code>[0,-1,-1,....,-1,1,-1,...,-1,1,...]</code>).</li>
<li>I first tried to implement <code>safefilter</code> as follow:
<pre>
safefilter' f l = if filter f (take 10000 l) == []
then []
else filter f l
</pre>
Explain why it doesn’t work and can enter into an infinite loop.</li>
<li>Suppose that <code>shuffle</code> is real random list with growing bounds. If you study a bit this structure, you’ll discover that with probability 1, this structure is finite. Using the following code (suppose we could use <code>safefilter'</code> directly as if was not in the where of safefilter) find a definition of <code>f</code> such that with probability <code>1</code>, <code>treeFromList' shuffle</code> is infinite. And prove it. Disclaimer, this is only a conjecture.</li>
</ul>
<divclass="sourceCode"id="cb202"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><aclass="sourceLine"id="cb202-1"title="1">treeFromList' [] n <spanclass="ot">=</span><spanclass="dt">Empty</span></a>
<aclass="sourceLine"id="cb202-2"title="2">treeFromList' (x<spanclass="op">:</span>xs) n <spanclass="ot">=</span><spanclass="dt">Node</span> x left right</a>
<p>Thanks to <ahref="http://reddit.com/r/haskell"><code>/r/haskell</code></a> and <ahref="http://reddit.com/r/programming"><code>/r/programming</code></a>. Your comment were most than welcome.</p>
<p>Particularly, I want to thank <ahref="https://github.com/Emm">Emm</a> a thousand times for the time he spent on correcting my English. Thank you man.</p>
<sectionclass="footnotes">
<hr/>
<ol>
<liid="fn1"><p>Even if most recent languages try to hide them, they are present.<ahref="#fnref1"class="footnote-back">↩</a></p></li>
<liid="fn2"><p>I know I’m cheating. But I will talk about non-strictness later.<ahref="#fnref2"class="footnote-back">↩</a></p></li>
<liid="fn3"><p>For the brave, a more complete explanation of pattern matching can be found <ahref="http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html">here</a>.<ahref="#fnref3"class="footnote-back">↩</a></p></li>
<liid="fn4"><p>Which is itself very similar to the javascript <code>eval</code> function, that is applied to a string containing JSON.<ahref="#fnref4"class="footnote-back">↩</a></p></li>
<liid="fn5"><p>There are some <em>unsafe</em> exceptions to this rule. But you shouldn’t see such use in a real application except maybe for debugging purposes.<ahref="#fnref5"class="footnote-back">↩</a></p></li>
<liid="fn6"><p>For the curious ones, the real type is <code>data IO a = IO {unIO :: State# RealWorld -> (# State# RealWorld, a #)}</code>. All the <code>#</code> has to do with optimisation and I swapped the fields in my example. But this is the basic idea.<ahref="#fnref6"class="footnote-back">↩</a></p></li>
<liid="fn7"><p>Well, you’ll certainly need to practice a bit to get used to them and to understand when you can use them and create your own. But you already made a big step in this direction.<ahref="#fnref7"class="footnote-back">↩</a></p></li>