<p>Haskell is functional language where data is immutable. This means that regular for-loops don’t really exist. Looping however is very common pattern in programs in general. Here are some patterns how to do that in Haskell.</p>
<h2 id="recursion">Recursion</h2>
<p>Calculating Fibonacci numbers is common example (sort of like hello world in Haskell). There’s many different implementations at https://wiki.haskell.org/The_Fibonacci_sequence if you’re interested on having a look.</p>
<p>Simple recursive definition:</p>
<pre><code>fibs :: Integer -&gt; Integer
fibs 0 = 0
fibs 1 = 1
fibs n = fibs (n-1) + fibs (n-2)</code></pre>
<p>When called with <code>0</code> result is <code>0</code>. When called with <code>1</code> result is <code>1</code>. For all other cases, <code>fibs</code> is called with values <code>n-1</code> and <code>n-1</code> and the results are summed together. This works fine when <code>n</code> is small, but calculation gets slow really quickly with bigger values.</p>
<p>Another way is to define list of all Fibonacci numbers recursively:</p>
<pre><code>allFibs :: [Integer]
allFibs = 0 : 1 : zipWith (+) allFibs (tail allFibs)</code></pre>
<p>Here a list is constructed. First element is <code>0</code>, second element is <code>1</code> and rest of the list is obtained by summing the list with its tail (everything but the first element of the list). Definition is recursive and defines all Fibonacci numbers. However, Haskell doesn’t evaluate whole list, but only as much of it as is required.</p>
<p>Common pattern of processing elements in a list, producing a new list:</p>
<pre><code>addOne :: [Integer] -&gt; [Integer]
addOne [] = []
addOne (x:xs) = x + 1 : addOne xs</code></pre>
<p>Two cases, when called with an empty list <code>[]</code>, result is empty list. For all other cases, list is taken apart <code>(x:xs)</code>, <code>x</code> contains first element of the list and <code>xs</code> is rest of the list. Body of the function creates a new list where head is <code>x + 1</code> and tail is <code>addOne xs</code>. This processes whole list of <code>Integer</code> by adding one to each value. It also reverses the list.</p>
<p>Second common pattern is processing a list and reducing it to a single value:</p>
<pre><code>sumAll :: Integer -&gt; [Integer] -&gt; Integer
sumAll n [] = n
sumAll n (x:xs) = sumAll (n + x) xs</code></pre>
<p>If given list is empty (the terminal case), result is <code>n</code>. Second case again takes list apart <code>(x:xs)</code>, adds <code>x</code> and <code>n</code> together and recursive call <code>sumAll</code> with tail of the list.</p>
<p>This common pattern is discarding some elements of a list:</p>
<pre><code>evenOnly :: [Integer] -&gt; [Integer]
evenOnly [] = []
evenOnly (x:xs) = 
    if even x
        then x : evenOnly xs
        else evenOnly xs</code></pre>
<p>Again, result of empty list is just empty list. In all other cases we first check if <code>x</code> is even. If so, new list is constructed where head is <code>x</code> and tail is <code>evenOnly xs</code>. If <code>x</code> isn’t even, it’s discarded and <code>evenOnly</code> is called recursively with tail of the list.</p>
<h2 id="more-tools">More tools</h2>
<p>Writing recursion by hand gets tedious and sometimes confusing (if you listened to the show, you probably noticed how I got confused and had to check that <code>evenOnly</code> actually works as I thought it would). For that reason, there are tools that abstract these common patterns and given them names.</p>
<p>First is <code>map</code>. It applies given function to each element of a list, thus producing a new list:</p>
<pre><code>&gt; map (+1) [1..10]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
&gt; map odd [1..10]
[True, False, True, False, True, False, True, False, True, False]</code></pre>
<p>Second is <code>fold</code>. There is good article at https://wiki.haskell.org/Foldr_Foldl_Foldl%27 that talks about differences between different folds.</p>
<p>The basic idea behind each fold is the same, they take a function and initial value and then apply them to first element of list, producing a value. This value is then applied with the function to the second element of the list and so on, until whole list has been reduced to a single value. Calculating a sum of list is so common operation that there’s specific function for that: <code>sum</code>.</p>
<pre><code>&gt; foldr (+) 0 [1..10]
55
&gt; foldl (+) 0 [1..10]
55
&gt; sum [1..10]
55</code></pre>
<p><code>scan</code> is similar to <code>fold</code>, except for returning only the final value, it also returns intermediate ones. Here it’s easier to observe how <code>scanr</code> and <code>scanl</code> differ from each other:</p>
<pre><code>&gt; scanr (+) 0 [1..10]
[55,54,52,49,45,40,34,27,19,10,0]
&gt; scanl (+) 0 [1..10]
[0,1,3,6,10,15,21,28,36,45,55]</code></pre>
<p>Last of the trifecta is <code>filter</code> that is used to select some of the elements in a list based on a supplied function.</p>
<pre><code>&gt; filter odd [1..10]
[1, 3, 5, 7, 9]
&gt; filter even [1..]
[2, 4, 6, 8, 10, 12, 14, 16...]
&gt; take 5 $ filter even [1..] 
[2, 4, 6, 8, 10]</code></pre>
<h2 id="even-more-tools">Even more tools</h2>
<p>There are even more tools at our disposal. Prelude is basic library of Haskell and browsing online documentation at https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html might yield interesting information.</p>
<p>For example, constructing some lists:</p>
<ul>
<li><code>iterate :: (a -&gt; a) -&gt; a -&gt; [a]</code> For list where function is applied repeatedly.</li>
<li><code>repeat :: a -&gt; [a]</code> for a list that contains infinite amount of <code>a</code>.</li>
<li><code>replicate :: Int -&gt; a -&gt; [a]</code> For a list that contains finite amount of <code>a</code>.</li>
<li><code>cycle :: [a] -&gt; [a]</code> For a infinite list that repeats same list over and over again.</li>
</ul>
<h2 id="finding-tools">Finding tools</h2>
<p>It’s all about knowing the right tools and finding them when needed. Luckily, you don’t have to memorize big stack of notes, but can turn to https://hoogle.haskell.org/ which is Haskell API search engine. It can search based on name or type signature. I often use it to find out if somebody has already written a function that I’m thinking of writing myself.</p>
<p>If you want to send questions or comments, I can be reached with email or at fediverse where I’m Tuula@mastodon.social. This episode is direct result of feedback that I got from previous one. If there’s Haskell topic you would love to hear more, drop me line or even better, research it by yourself and make a cool Hacker Public Radio episode.</p>
