<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[dave^2 = -1]]></title>
  <link href="http://davesquared.net/atom.xml" rel="self"/>
  <link href="http://davesquared.net/"/>
  <updated>2012-05-15T18:04:58+10:00</updated>
  <id>http://davesquared.net/</id>
  <author>
    <name><![CDATA[David Tchepak]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[FP newbie learns a little about applicatives]]></title>
    <link href="http://davesquared.net/2012/05/fp-newbie-learns-applicatives.html"/>
    <updated>2012-05-15T17:47:00+10:00</updated>
    <id>http://davesquared.net/2012/05/fp-newbie-learns-applicatives</id>
    <content type="html"><![CDATA[<p>A few posts back I learned that <a href="http://davesquared.net/2012/05/fp-newbie-learns-functors.html">functors are types that can be mapped over</a>. The idea is that if we have a function <code>a -&gt; b</code>, we would like to be able to apply that to <code>a</code>s in different contexts, such as a list of <code>a</code>, a <code>Maybe a</code>, or an IO action that results in an <code>a</code>. In the previous post we referred to these contexts as &quot;boxes&quot;, so we could lift a function <code>a -&gt; b</code> to work on a box of <code>a</code> and return a box of <code>b</code>.</p>
<pre><code>-- &quot;Functor f =&gt;&quot;  just means that `f` refers to a functor type (or a type of box)
fmap :: Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b 

ghci&gt; fmap succ (Just 4)
Just 5
ghci&gt; fmap (^2) (Just 4)
Just 16
ghci&gt; fmap (++&quot;!&quot;) getLine
Hello World
&quot;Hello World!&quot;</code></pre>
<p>All these calls map single argument functions over functors, which is neat, but a bit limiting. What happens if we map a two (or more) argument function like <code>+</code>?</p>
<pre><code>ghci&gt; :t fmap (+) (Just 4)
fmap (+) (Just 4) :: Num a =&gt; Maybe (a -&gt; a)</code></pre>
<p>This gives us a <code>+4</code> function in a <code>Maybe</code> context, so <code>fmap (+) (Just 4) = Just (4+)</code>. But how do we pass the second argument to this boxed up function? We can&#8217;t use <code>fmap</code> again, because it&#8217;s signature takes an <code>(a -&gt; b)</code>, not a <code>f (a -&gt; b)</code>. But if <code>f</code> is not just a functor, but an <em>applicative functor</em>, then we have another option. Applicative functors still support <code>fmap</code>, but also add some other functions, the main one being:</p>
<pre><code>(&lt;*&gt;) :: Applicative f =&gt; f (a -&gt; b) -&gt; f a -&gt; f b</code></pre>
<p>The bizarre-looking <code>&lt;*&gt;</code> function takes a boxed-up function <code>a -&gt; b</code> and applies it to a boxed-up <code>a</code>, which is just what we need in this case.</p>
<!-- more -->

<pre><code>ghci&gt; import Control.Applicative  -- imports (&lt;*&gt;) function (et al.)
ghci&gt; fmap (+) (Just 4) &lt;*&gt; (Just 10)
Just 14</code></pre>
<h2 id="applying-applicatives">Applying applicatives</h2>
<p>Just as <code>fmap</code> lets us map a function over a value in a context, <code>&lt;*&gt;</code> applies a function in a context to a value in a context. It ends up mirroring standard function application, only within a context:</p>
<pre><code>ghci&gt; let sum3 a b c = a+b+c
ghci&gt; sum3 5 10 15
30
ghci&gt; fmap sum3 (Just 5) &lt;*&gt; (Just 10) &lt;*&gt; (Just 15)
Just 30</code></pre>
<p>The initial <code>fmap</code> call lifts <code>sum3</code> into the new context (puts it in a box, in this case the <code>Maybe</code> type), then we apply that boxed function to its remaining arguments while staying in that context.</p>
<p>Because this is a fairly common pattern, the <code>Control.Applicative</code> library defines the function <code>&lt;$&gt;</code> as an alias for <code>fmap</code>:</p>
<pre><code>ghci&gt; sum3 &lt;$&gt; (Just 5) &lt;*&gt; (Just 10) &lt;*&gt; (Just 15)
Just 30</code></pre>
<p>Using this syntax we can apply functions with any number of arguments within a context. We apply each argument using <code>&lt;*&gt;</code>.</p>
<h2 id="lifting-functions">Lifting functions</h2>
<p>There are also some convenience functions, <code>liftA</code>, <code>liftA2</code> and <code>liftA3</code> to convert 1, 2 and 3 argument functions that take applicative functor values. From the <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Control-Applicative.html#liftA">Haskell source</a>:</p>
<pre><code>-- For unary functions:
liftA :: Applicative f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
liftA f a = pure f &lt;*&gt; a

-- For binary functions:
liftA2 :: Applicative f =&gt; (a -&gt; b -&gt; c) -&gt; f a -&gt; f b -&gt; f c
liftA2 f a b = f &lt;$&gt; a &lt;*&gt; b

-- For ternary functions:
liftA3 :: Applicative f =&gt; (a -&gt; b -&gt; c -&gt; d) -&gt; f a -&gt; f b -&gt; f c -&gt; f d
liftA3 f a b c = f &lt;$&gt; a &lt;*&gt; b &lt;*&gt; c</code></pre>
<p>This lets us use standard function syntax for calling our functions within a context.</p>
<pre><code>ghci&gt; liftA3 sum3 (Just 5) (Just 10) (Just 15)
Just 30</code></pre>
<h2 id="well-behaved-applicatives">Well-behaved applicatives</h2>
<p>Just as there are <a href="http://davesquared.net/2012/05/fp-newbie-learns-functors.html#well-behaved-functors">laws a functor must satisfy</a>, applicative functors have some additional requirements.</p>
<p>First up, as they are defined in Haskell, applicatives have to not only supply a <code>&lt;*&gt;</code> function, but also a <code>pure</code> function, which will take a value and put it in the context of the applicative functor:</p>
<pre><code>pure :: Applicative f =&gt; a -&gt; f a</code></pre>
<p>This can be used to lift a function into a context so we can apply it to applicatives. As above, <code>liftA</code> is defined as <code>liftA f a = pure f &lt;*&gt; a</code>, which is equivalent to writing <code>fmap f a</code>. This gives our first applicative property. There&#8217;s a good description of <a href="http://en.wikibooks.org/wiki/Haskell/Applicative_Functors">all the applicative laws in the Haskell Wikibook</a>, but as a quick summary:</p>
<ul>
<li>fmap law: <code>pure f &lt;*&gt; x = fmap f x</code></li>
<li>Identity: <code>pure id &lt;*&gt; v = v</code></li>
<li>Composition: <code>pure (.) &lt;*&gt; u &lt;*&gt; v *&lt;&gt; w = u &lt;*&gt; (v &lt;*&gt; w)</code></li>
<li>Interchange: <code>u &lt;*&gt; pure y = pure ($ y) &lt;*&gt; u</code></li>
<li>Homomorphism: <code>pure f &lt;*&gt; pure x = pure (f x)</code></li>
</ul>
<h2 id="summary">Summary</h2>
<p>Applicative functors are functors that support some additional behaviours. While the motivation for functors is to be able to apply a function within a context, the motivation for applicatives is to apply functions with multiple arguments within a context.</p>
<p>We can apply each argument using the <code>&lt;*&gt;</code> operator, giving us expressions like <code>f &lt;$&gt; arg0 &lt;*&gt; arg1 &lt;*&gt; ... &lt;*&gt; argN</code>. We can also lift functions of 1 to 3 arguments using <code>liftA</code>, <code>liftA2</code> or <code>liftA3</code>.</p>
<p>We saw previously that <a href="http://davesquared.net/2012/05/fp-newbie-learns-functors.html#lifting-functions-is-useful">functors can be quite useful</a> to reuse functions in different contexts, such as when performing IO operations that result in particular values. Applicative functors extends the same benefits to functions that take multiple arguments.</p>]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Towards point-free redux]]></title>
    <link href="http://davesquared.net/2012/05/towards-point-free-redux.html"/>
    <updated>2012-05-13T21:58:00+10:00</updated>
    <id>http://davesquared.net/2012/05/towards-point-free-redux</id>
    <content type="html"><![CDATA[<p>Over the <a href="http://davesquared.net/2012/05/towards-point-free-in-csharp.html">last</a> <a href="http://davesquared.net/2012/05/lengthy-approach-to-haskell.html">two</a> posts I&#8217;ve made a few efforts to explain how we can drop of explicit argument references when moving functions towards point-free style. We&#8217;ve looked at currying, partial function application, and function composition as explanations for this. These concepts are really important, but after kicking around various ideas about this with a colleague I&#8217;ve decided to try and separate all these concepts from the most fundamental form of reducing a function towards point-free.</p>
<!-- more -->

<h2 id="eta-reduction">Eta reduction</h2>
<p>Say we have two functions, <code>f</code> and <code>g</code>, with exactly the same types. Both take one argument of type <code>a</code> and return something of type <code>b</code>. We&#8217;ll define <code>f</code> in terms of <code>g</code>:</p>
<pre><code>f :: a -&gt; b
g :: a -&gt; b

f = \a -&gt; g a</code></pre>
<p>In Haskell this this can also be written as:</p>
<pre><code>f a = g a       ... (1)</code></pre>
<p>If <code>f</code> and <code>g</code> are <a href="http://en.wikipedia.org/wiki/Pure_function">pure functions</a> (deterministic, no side-effects, no state), then we have:</p>
<pre><code>f = g           ... (2)</code></pre>
<p>We&#8217;ve just said in <code>(1)</code> that the functions are equal when applied with the same argument, so for all values in type <code>a</code> the functions <code>f</code> and <code>g</code> will be equivalent; we can use them <a href="http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)">interchangeably</a>. We don&#8217;t have to worry about partial application, or currying, or anything else; we can just apply this equivalency.</p>
<p>Removing redundant argument references like this is called <a href="http://www.haskell.org/haskellwiki/Eta_conversion">eta reduction</a> (also written as <em>η-reduction</em>). Continually reducing in this way can lead to point-free functions once we&#8217;ve omitted all redundant argument references.</p>
<h2 id="applying-with-multiple-arguments">Applying with multiple arguments</h2>
<p>We can apply this same equivalency to our <a href="http://davesquared.net/2012/05/towards-point-free-in-csharp.html">previous example</a> with multiple arguments:</p>
<pre><code>z = \acc x -&gt; const (acc+1) x

-- `z` is of the form:  
--      f x = g x 
-- where:
--      f = z acc
--      g = const (acc+1)

z = \acc -&gt; const (acc+1)</code></pre>
<p>We use currying, partial application, and concepts like function composition to get our functions into the required <code>f x = g x</code> form, but once we&#8217;re there we can use η-reduction to drop the explicit reference to the last argument.</p>
<h2 id="impure-functions">Impure functions</h2>
<p>Notice that this doesn&#8217;t apply for impure functions, as we could have:</p>
<pre><code>int f(int x) { Console.WriteLine(x); return g(x); }
int g(int x) { return x+1; }</code></pre>
<p>In this case we can&#8217;t use <code>f</code> and <code>g</code> interchangeably as they behave differently. In a pure functional language like Haskell we don&#8217;t have to worry about this; the type system will differentiate functions with side-effects.</p>]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Towards point-free in C#]]></title>
    <link href="http://davesquared.net/2012/05/towards-point-free-in-csharp.html"/>
    <updated>2012-05-12T23:37:00+10:00</updated>
    <id>http://davesquared.net/2012/05/towards-point-free-in-csharp</id>
    <content type="html"><![CDATA[<p>Last post we saw <a href="http://davesquared.net/2012/05/lengthy-approach-to-haskell.html">an example of writing a function to get the length of a list</a>, first using explicit recursion, then folds, then moving towards <em>point-free</em> style by dropping explicit references to arguments where practical. To summarise the latter part of that post:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">length</span> <span class="n">xs</span> <span class="ow">=</span> <span class="n">foldl&#39;</span> <span class="p">(</span><span class="nf">\</span><span class="n">acc</span> <span class="n">x</span> <span class="ow">-&gt;</span> <span class="n">acc</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="mi">0</span> <span class="n">xs</span>
</span><span class='line'><span class="nf">length</span> <span class="ow">=</span> <span class="n">foldl&#39;</span> <span class="p">(</span><span class="nf">\</span><span class="n">acc</span> <span class="n">x</span> <span class="ow">-&gt;</span> <span class="n">acc</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="mi">0</span>            <span class="c1">-- `xs` appears on both sides, so can drop it.</span>
</span><span class='line'><span class="nf">length</span> <span class="ow">=</span> <span class="n">foldl&#39;</span> <span class="p">(</span><span class="nf">\</span><span class="n">acc</span> <span class="n">x</span> <span class="ow">-&gt;</span> <span class="n">const</span> <span class="p">(</span><span class="n">acc</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="n">x</span><span class="p">)</span> <span class="mi">0</span>  <span class="c1">-- `const` ignores 2nd arg, returns 1st.</span>
</span><span class='line'><span class="nf">length</span> <span class="ow">=</span> <span class="n">foldl&#39;</span> <span class="p">(</span><span class="nf">\</span><span class="n">acc</span> <span class="ow">-&gt;</span> <span class="n">const</span> <span class="p">(</span><span class="n">acc</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span> <span class="mi">0</span>      <span class="c1">-- Drop `x` from both sides of lambda</span>
</span><span class='line'><span class="nf">length</span> <span class="ow">=</span> <span class="n">foldl&#39;</span> <span class="p">(</span><span class="nf">\</span><span class="n">acc</span> <span class="ow">-&gt;</span> <span class="n">const</span> <span class="p">(</span><span class="n">succ</span> <span class="n">acc</span><span class="p">))</span> <span class="mi">0</span>   <span class="c1">-- Replace (+1) with built-in `succ` function</span>
</span><span class='line'><span class="nf">length</span> <span class="ow">=</span> <span class="n">foldl&#39;</span> <span class="p">(</span><span class="nf">\</span><span class="n">acc</span> <span class="ow">-&gt;</span> <span class="p">(</span><span class="n">const</span><span class="o">.</span><span class="n">succ</span><span class="p">)</span> <span class="n">acc</span><span class="p">)</span> <span class="mi">0</span>   <span class="c1">-- Function composition rule: (f.g) x = f(g x)</span>
</span><span class='line'><span class="nf">length</span> <span class="ow">=</span> <span class="n">foldl&#39;</span> <span class="p">(</span><span class="n">const</span><span class="o">.</span><span class="n">succ</span><span class="p">)</span> <span class="mi">0</span>                 <span class="c1">-- Drop `acc` from both sides of lambda</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>The topic of this post is the &quot;argument appears on both sides so can drop it&quot; steps. How do we go from passing <code>foldl'</code> a function which takes two explicit arguments (<code>\arg x -&gt; ...</code>) to none? The answer is by using <em>currying</em>, <em>partial function application</em> and <em>function composition</em>, and we can do both of these in C# (albeit not as neatly, as C# is not really built for it).</p>
<!-- more -->

<h2 id="standard-c">Standard C#</h2>
<p>We can implement the first version of the Haskell <code>length</code> function above using the following C#:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="n">A</span> <span class="n">Foldl</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="p">&gt;(</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="p">,</span><span class="n">A</span><span class="p">&gt;</span> <span class="n">f</span><span class="p">,</span> <span class="n">A</span> <span class="n">initial</span><span class="p">,</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">B</span><span class="p">&gt;</span> <span class="n">xs</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">acc</span> <span class="p">=</span> <span class="n">initial</span><span class="p">;</span>
</span><span class='line'>    <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">x</span> <span class="k">in</span> <span class="n">xs</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">acc</span> <span class="p">=</span> <span class="n">f</span><span class="p">(</span><span class="n">acc</span><span class="p">,</span> <span class="n">x</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">acc</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="na">[Test]</span>
</span><span class='line'><span class="k">public</span> <span class="k">void</span> <span class="nf">TestLength0</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">xs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">4</span><span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="kt">var</span> <span class="n">length0</span> <span class="p">=</span> <span class="n">Foldl</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">&gt;((</span><span class="n">acc</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">acc</span><span class="p">+</span><span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="n">xs</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="n">length0</span><span class="p">,</span> <span class="m">4</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>So the <code>Foldl</code> function loops over a list, and accumulates a value based on a function <code>f</code> passed in. To get the length of a list, our <code>f</code> is going to add 1 to our accumulator for each element in the list.</p>
<h2 id="currying">Currying</h2>
<p>Our <code>Foldl</code>, much like Haskell&#8217;s, takes a function of the form <code>Func&lt;A,B,A&gt;</code>. It takes two arguments, the accumulator <code>acc</code> and the current list item <code>x</code>, and returns a new accumulator value. In Haskell though, functions all take a single argument and return a function that will handle the rest. This is called <em>currying</em>.</p>
<p>To do this in C# we need to convert <code>Func&lt;A,B,A&gt;</code> into <code>Func&lt;A,Func&lt;B,A&gt;&gt;</code>. In other words, rather than taking two arguments and returning a value, our function will take one argument, then return a function which takes the second argument and finally return the required value. We can write an overload of <code>Foldl</code> to support curried functions:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="n">A</span> <span class="n">Foldl</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="p">&gt;(</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">B</span><span class="p">,</span><span class="n">A</span><span class="p">&gt;&gt;</span> <span class="n">curriedFn</span><span class="p">,</span> <span class="n">A</span> <span class="n">initial</span><span class="p">,</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">B</span><span class="p">&gt;</span> <span class="n">xs</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">acc</span> <span class="p">=</span> <span class="n">initial</span><span class="p">;</span>
</span><span class='line'>    <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">x</span> <span class="k">in</span> <span class="n">xs</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="kt">var</span> <span class="n">f</span> <span class="p">=</span> <span class="n">curriedFn</span><span class="p">(</span><span class="n">acc</span><span class="p">);</span> <span class="c1">// Call with acc to get remainder of function</span>
</span><span class='line'>        <span class="n">acc</span> <span class="p">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>             <span class="c1">// Apply last argument to function</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">acc</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="na">[Test]</span>
</span><span class='line'><span class="k">public</span> <span class="k">void</span> <span class="nf">TestLength1</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">xs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">4</span><span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="kt">var</span> <span class="n">length1</span> <span class="p">=</span> <span class="n">Foldl</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">&gt;(</span><span class="n">acc</span> <span class="p">=&gt;</span> <span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span> <span class="n">acc</span><span class="p">+</span><span class="m">1</span><span class="p">),</span> <span class="m">0</span><span class="p">,</span> <span class="n">xs</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="n">length1</span><span class="p">,</span> <span class="m">4</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>Now we need to pass this crazy looking <code>acc =&gt; (x =&gt; acc+1)</code> line to our fold, but we end up with two functions that each take a single argument.</p>
<h2 id="dropping-the-reference-to-x">Dropping the reference to <code>x</code></h2>
<p>Let&#8217;s go back to using the original <code>Foldl</code> overload that takes a two argument <code>Func&lt;A,B,A&gt;</code>, and introduce a <code>Const</code> function like we did for the Haskell example:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="c1">// `const` should actually be `Func&lt;B,A&gt; Const(A a)`, but we&#39;re using `int`</span>
</span><span class='line'><span class="c1">// as that&#39;s all we need for this example, and it makes the type defs less verbose.</span>
</span><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="n">Func</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">&gt;</span> <span class="n">Const</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">b</span> <span class="p">=&gt;</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="na">[Test]</span>
</span><span class='line'><span class="k">public</span> <span class="k">void</span> <span class="nf">TestLength2</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">xs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">4</span><span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="kt">var</span> <span class="n">length2</span> <span class="p">=</span> <span class="n">Foldl</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">&gt;((</span><span class="n">acc</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">Const</span><span class="p">(</span><span class="n">acc</span><span class="p">+</span><span class="m">1</span><span class="p">)(</span><span class="n">x</span><span class="p">),</span> <span class="m">0</span><span class="p">,</span> <span class="n">xs</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="n">length2</span><span class="p">,</span> <span class="m">4</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>The <code>Const</code> function is curried; it takes one argument and returns a new function which takes a second argument, ignores it and returns the original, which is why it is called as <code>Const(acc+1)(x)</code>. We could have also written it like this:</p>
<pre><code>(acc,x) =&gt; {
    var newFn = Const(acc+1);
    return newFn(x);
}</code></pre>
<p>Sure, doesn&#8217;t make too much sense for C#, but when you get it for free in languages like Haskell it can let us do some interesting stuff. Interesting stuff like partial function application.</p>
<p>We&#8217;re currently passing a lambda <code>(acc,x) =&gt; Const(acc+1)(x)</code>, which is of type <code>Func&lt;int,int,int&gt;</code>. What would happen if we didn&#8217;t give this function the <code>x</code> argument?</p>
<pre><code>acc =&gt; Const(acc+1)</code></pre>
<p>We know our curried <code>Const</code> function returns a <code>Func&lt;int,int&gt;</code>. And we also know that <code>acc</code> is an <code>int</code>. So our lambda <code>acc =&gt; Const(acc+1)</code> will be a <code>Func&lt;int, Func&lt;int,int&gt;&gt;</code>. And we have an overload of <code>Foldl</code> that will take a curried function of type <code>Func&lt;A, Func&lt;B,A&gt;&gt;</code>, so maybe we can use that?</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="na">[Test]</span>
</span><span class='line'><span class="k">public</span> <span class="k">void</span> <span class="nf">TestLength3</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">xs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">4</span><span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="kt">var</span> <span class="n">length3</span> <span class="p">=</span> <span class="n">Foldl</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">&gt;(</span><span class="n">acc</span> <span class="p">=&gt;</span> <span class="n">Const</span><span class="p">(</span><span class="n">acc</span><span class="p">+</span><span class="m">1</span><span class="p">),</span> <span class="m">0</span><span class="p">,</span> <span class="n">xs</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="n">length3</span><span class="p">,</span> <span class="m">4</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>The <code>Const</code> function needs to eventually get two arguments to return a result (e.g. <code>Const(acc+1)(x)</code>), but instead we&#8217;re only applying one of the arguments (<code>Const(acc+1)</code>), and passing around the resulting function that is patiently waiting for the second argument before it returns its result. Because it is in this semi-evaluated state, we call this <em>partial function application</em>.</p>
<p>So where does the second argument come from?</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="n">A</span> <span class="n">Foldl</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="p">&gt;(</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">B</span><span class="p">,</span><span class="n">A</span><span class="p">&gt;&gt;</span> <span class="n">curriedFn</span><span class="p">,</span> <span class="n">A</span> <span class="n">initial</span><span class="p">,</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">B</span><span class="p">&gt;</span> <span class="n">xs</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">acc</span> <span class="p">=</span> <span class="n">initial</span><span class="p">;</span>
</span><span class='line'>    <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">x</span> <span class="k">in</span> <span class="n">xs</span><span class="p">)</span> <span class="p">{</span>     <span class="c1">// &lt;--- We get an `x` from the list `xs`</span>
</span><span class='line'>        <span class="kt">var</span> <span class="n">f</span> <span class="p">=</span> <span class="n">curriedFn</span><span class="p">(</span><span class="n">acc</span><span class="p">);</span> <span class="c1">// &lt;--- Call `curriedFn` with first argument</span>
</span><span class='line'>        <span class="n">acc</span> <span class="p">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>             <span class="c1">// &lt;--- Apply `x` as second argument</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">acc</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>This corresponds to the following step from our original Haskell version:</p>
<pre><code>length = foldl&#39; (\acc x -&gt; const (acc+1) x) 0  -- `const` ignores 2nd arg, returns 1st.
length = foldl&#39; (\acc -&gt; const (acc+1)) 0      -- Drop `x` from both sides of lambda</code></pre>
<h2 id="matching-experiments-with-intuition">Matching experiments with intuition</h2>
<p>How can we just lose an argument like that? Well, if the function we&#8217;re calling is expecting to use a function with two arguments, it&#8217;s going to call it with two arguments. If we pass <code>Foldl</code> a curried function that references both arguments and returns a value like <code>acc =&gt; (x =&gt; acc+1)</code>, it will call <code>curriedFn(acc)(x)</code>. If instead we pass it a partially-applied function that returns a new function like <code>acc =&gt; Const(acc+1)</code>, <code>Foldl</code> is still going to call <code>curriedFn(acc)(x)</code>, which applies the second argument to the new function.</p>
<p>In Haskell all functions are curried, so regardless of whether we pass a function <code>f</code> as <code>\acc x -&gt; const acc x</code> or <code>\acc -&gt; const acc</code>, we&#8217;re still going to call <code>(f a) b</code> and get the same result.</p>
<p>The end result is that for curried functions, if we have a free variable on the end of both sides of an expression, we don&#8217;t have to explicitly reference it. We can drop it off the argument list on the left of the expression at the same time as we stop applying it on the right. So for a Haskell function like this:</p>
<pre><code>  z = fn (\x y -&gt; (someFn x) y)          -- y free on left and right of `-&gt;`
    = fn (\x -&gt; someFn x)                -- x free on left and right of `-&gt;`
    = fn someFn</code></pre>
<div class="note">
<strong>Aside:</strong> If I&#8217;m getting my FP-<a href="http://en.wikipedia.org/wiki/Gobbledygook">gobbledygook</a> right, dropping an explicit argument reference like this is known as <a href="http://www.haskell.org/haskellwiki/Eta_conversion">eta reduction</a>.
</div>

<h2 id="finishing-up-with-function-composition">Finishing up with function composition</h2>
<p>The final steps in our Haskell example where to replace <code>acc+1</code> with a call to <code>succ acc</code> (which does exactly the same thing, but it looks like a normal function compared with <code>(+1)</code>), and then use function composition to remove the remaining arguments in our lambda.</p>
<p>We can do this, again a tad messily, in C# by introducing our own <code>Succ</code> and <code>Compose</code> functions:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="n">Func</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">C</span><span class="p">&gt;</span> <span class="n">Compose</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="p">,</span><span class="n">C</span><span class="p">&gt;(</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">B</span><span class="p">,</span><span class="n">C</span><span class="p">&gt;</span> <span class="n">f</span><span class="p">,</span> <span class="n">Func</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="p">&gt;</span> <span class="n">g</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">//Function composition rule: (f.g) x = f(g x)</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">x</span> <span class="p">=&gt;</span> <span class="n">f</span><span class="p">(</span><span class="n">g</span><span class="p">(</span><span class="n">x</span><span class="p">));</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Succ</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">x</span><span class="p">+</span><span class="m">1</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="na">[Test]</span>
</span><span class='line'><span class="k">public</span> <span class="k">void</span> <span class="nf">TestLength4</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">xs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">4</span><span class="p">};</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">length4</span> <span class="p">=</span> <span class="n">Foldl</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">&gt;(</span><span class="n">acc</span> <span class="p">=&gt;</span> <span class="n">Compose</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">,</span><span class="n">Func</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">&gt;&gt;(</span><span class="n">Const</span><span class="p">,</span><span class="n">Succ</span><span class="p">)(</span><span class="n">acc</span><span class="p">),</span> <span class="m">0</span><span class="p">,</span> <span class="n">xs</span><span class="p">);</span>
</span><span class='line'>    <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="n">length4</span><span class="p">,</span> <span class="m">4</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>Ugh, C#&#8217;s type inference doesn&#8217;t quite go far enough to help us with that awful generic signature, but we&#8217;ve now replaced a <code>acc =&gt; Const(Succ(acc))</code> function with <code>acc =&gt; Compose&lt;...&gt;(Const,Succ)(acc)</code>. And now we&#8217;ve got the <code>acc</code> argument free on both sides, so we can drop it:</p>
<pre><code>[Test]
public void TestLength4() {
    var xs = new[] {1,2,3,4};
    var length5 = Foldl&lt;int,int&gt;(Compose&lt;int,int,Func&lt;int,int&gt;&gt;(Const,Succ), 0, xs);
    Assert.AreEqual(length4, 4);
}</code></pre>
<h2 id="conclusion">Conclusion</h2>
<p>We&#8217;ve seen that when passing curried functions, if an argument appears at the end of the function&#8217;s argument list and it is applied unchanged at the end of the function definition, we can drop it off both and pass around the partially applied function. So:</p>
<pre><code>acc =&gt; (x =&gt; Const(acc+1)(x))</code></pre>
<p>Is the same as:</p>
<pre><code>acc =&gt; Const(acc+1)</code></pre>
<p>This stuff doesn&#8217;t translate all that well to C#, but for languages like Haskell where all functions are curried we get equivalences like this:</p>
<pre><code>f = \acc x =&gt; const (acc+1) x
  = \acc =&gt; const (acc+1)
  = \acc =&gt; const (succ acc)
  = \acc =&gt; (const . succ) acc
  = const . succ</code></pre>
<p>Hopefully this will help if you&#8217;ve found playing around with higher order functions in Haskell as challenging as I have. If you&#8217;ve got any questions or if I&#8217;ve got anything muddled up please let me know in the comments.</p>]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A lengthy approach to Haskell fundamentals]]></title>
    <link href="http://davesquared.net/2012/05/lengthy-approach-to-haskell.html"/>
    <updated>2012-05-06T20:50:00+10:00</updated>
    <id>http://davesquared.net/2012/05/lengthy-approach-to-haskell</id>
    <content type="html"><![CDATA[<p>At my work a few of us developers are learning Haskell, starting with some exercises from <a href="http://twitter.com/dibblego">Tony Morris&#8217;s</a> YOW workshop last year. One of these exercises involves re-implementing some common operations on lists, and this has proved a great way to learn some of the basics of Haskell.</p>
<p>For this post we&#8217;ll be working through implementing our own version of the <code>length</code> function, which takes a list and returns a integer reflecting the length of that list.</p>
<p>We&#8217;ll start with this non-functioning implementation, which we&#8217;ll call <code>length'</code> so it doesn&#8217;t collide with Haskell&#8217;s built-in function (<code>'</code> is a legal character to have in Haskell identifiers):</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; = error &quot;todo&quot;</code></pre>
<!-- more -->

<p>To run our function, save it in <code>length.hs</code>, and run <code>ghci length.hs</code> to load it into GHCi, the Haskell REPL. If you update the file, run <code>:r</code> to reload it. See &quot;Loading a Haskell file&quot; in my <a href="http://davesquared.net/2012/02/haskell-newbie-attempts-a-haskell-quick-start.html">Haskell quickstart</a> for more information on setting this up.</p>
<p>If we run this with GHCi we get:</p>
<pre><code>ghci&gt; length&#39; [1,2,3]
*** Exception: todo</code></pre>
<p>Great, let&#8217;s get started.</p>
<h2 id="type-signatures">Type signatures</h2>
<p>The first line is the type signature of our function. The <code>::</code> means &quot;has type&quot;<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup>. To the right of <code>::</code> are the function argument types, and the return type. In this case we can see that <code>length'</code> takes <code>[a]</code> (a list of <code>a</code>) and returns an <code>Int</code>.</p>
<p><code>Int</code> is pretty clear, but what&#8217;s <code>a</code>? It is a <em>type parameter</em>, which means it can stand in for any type. We don&#8217;t mind what kind of type is in the list passed to <code>length'</code>, just so long as we get a list. This is analogous to <code>List&lt;T&gt;</code> in C#.</p>
<p>Regardless of whether we have <code>[1,2,3]</code>, <code>[&quot;hello&quot;, &quot;world&quot;]</code> or the empty list <code>[]</code>, we can still work out the length of the list without knowing what type of elements are in it.</p>
<h2 id="functions-with-multiple-arguments">Functions with multiple arguments</h2>
<p>Our <code>length'</code> function only takes one argument. When we have multiple arguments we separate them with multiple <code>-&gt;</code> operators. One example is the built-in <code>const</code> function. We can implement our own version like this:</p>
<pre><code>const&#39; :: a -&gt; b -&gt; a
const&#39; first second = first</code></pre>
<p>This takes an <code>a</code> and a <code>b</code> as an argument, and returns an <code>a</code>. The type signature gives us a fair idea of what it is doing without reading the implementation: <code>const'</code> takes two arguments, and returns the first one. This is exactly what the implementation does. The left-hand side of the <code>=</code> is the function name and argument names, and the right-hand side is the value that the function will evaluated to.</p>
<div class="note">
<strong>Aside:</strong> I wrote &#8216;evaluated&#8217; rather than &#8216;it returns&#8217; to highlight an at first subtle difference between imperative and functional programming. In imperative programs we step through a series of instructions in a procedure and return a value. In functional programming, functions get evaluated and reduced according to their definitions. It&#8217;s a lot like algebra: if we have <code>y = x^2</code>, and <code>x = 2*z</code>, we can evaluate <code>y</code> as <code>4*z^2</code>. We&#8217;re not executing instructions so much as substituting rules to evaluate functions to their simplest form.
</div>

<p>Notice that the first argument to <code>const'</code> is some type <code>a</code>, while the second is some type <code>b</code>. Again, this means that <code>a</code> and <code>b</code> can be any types, but it also means that they don&#8217;t have to have any particular relationship. If the type was <code>a -&gt; a -&gt; a</code>, then all the arguments and the return value would have to be of the same type. By having <code>a</code> and <code>b</code>, we&#8217;re saying that while they could both be the same type, they could also vary independently. We could pass an <code>Int</code> and <code>String</code> for example. We&#8217;ve also said the return value is an <code>a</code>, so that means it will have to return a value that is the same type as the first argument.</p>
<p>Let&#8217;s experiment a little with our <code>const'</code> function:</p>
<pre><code>ghci&gt; const&#39; 42 100
42
ghci&gt; const&#39; &quot;hello&quot; 12
&quot;hello&quot;
ghci&gt; const&#39; &#39;a&#39; [1,2,3]
&#39;a&#39;</code></pre>
<p>We can see that arguments are separated by spaces when passed to functions. Rather than writing <code>f(x,y,z)</code>, we write <code>f x y z</code>.</p>
<h2 id="pattern-matching">Pattern matching</h2>
<p>Let&#8217;s get back to our <code>length'</code> function. To start, let&#8217;s consider the length of an empty list <code>[]</code>.</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; [] = 0</code></pre>
<p>We&#8217;re telling Haskell that when it needs to evaluate an expression <code>length' []</code>, it can replace it with <code>0</code>.</p>
<pre><code>ghci&gt; length&#39; []
0
ghci&gt; length&#39; [42]
*** Exception: lengthy.hs:2:1-14: Non-exhaustive patterns in function length&#39;</code></pre>
<p>This works fine with the empty list, but Haskell doesn&#8217;t know how to evaluate the expression when it has a list that doesn&#8217;t match the pattern we gave it (in this case, it fails to match a list with a single element in it). We can make our function handle additional cases by providing other patterns it can match. Haskell will try each pattern, from top to bottom, and evaluate the first one that matches (or raise an exception about non-exhaustive patterns if there is no match).</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; [] = 0
length&#39; [a] = 1
length&#39; [b,a] = 2

ghci&gt; length&#39; [42]
1
ghci&gt; length&#39; [42,24]
2
ghci&gt; length&#39; [42,24,37]
*** Exception: lengthy.hs:(2,1)-(4,17): Non-exhaustive patterns in function length&#39;</code></pre>
<p>This seems to be working, but this approach isn&#8217;t exactly going to scale well to all possible inputs. Luckily for us, Haskell can do some more advanced matching based on the structure of the data type being passed to the function.</p>
<h2 id="the-list-type">The list type</h2>
<p>The list data type we&#8217;re using is made up of two different types of values. <code>[a]</code> can either be the empty list <code>[]</code>, or be an element joined to the front of a <code>[a]</code> using a <code>:</code>, or <em>cons</em> operator (for <em>construct</em> I think). Its definition would probably look a bit like this (where the <code>|</code> means <code>or</code>).</p>
<pre><code>data [a] = [] | a : [a]</code></pre>
<p>At first this sounds a bit strange: a list is either empty or an element and a list? But if we step through it, it actually makes a lot of sense. <code>[]</code> is a valid list, so <code>1:[]</code> is also a valid list (an element <em>cons&#8217;d</em> on to another list). Which means we could also do <code>2:(1:[])</code>, and <code>3:(2:(1:[]))</code>, and so on. This is a <em>recursive</em> definition; a list is defined by an element cons&#8217;d on to a list. The recursion terminates when we get to the empty list <code>[]</code>.</p>
<div class="note">
<strong>Aside:</strong> GHCi prints <code>1:(2:(3:[]))</code> as <code>[1,2,3]</code>, the latter just being a prettified version of the former. Because <code>:</code> is <a href="http://davesquared.net/2012/04/associativity.html/">right-associative</a>, we can also write it as <code>1:2:3:[]</code>. Finally, because lists are so common, Haskell has some short-hand forms for defining lists, such as <code>[1,2,3]</code> as we&#8217;ve already seen, but also <code>[1..10]</code> for a list from one to ten, <code>[2,4..10]</code> gives even numbers from two to ten, and <code>[10..]</code> gives a list that starts at ten and goes on toward infinity.
</div>

<p>We now know enough to be able to specify all the patterns we need for our <code>length'</code> function. Our input list will either be <code>[]</code>, or in the form <code>a : [a]</code>. Together these two patterns cover every list that can be passed to our function (in other words, they <em>exhaustively</em> cover all the inputs).</p>
<p>The first element in the pattern <code>a : [a]</code> is called the <em>head</em> of the list, while the second is called the <em>tail</em> (i.e. the rest). We can think of list <code>[1,2,3]</code> as <code>1:[2,3]</code> (or <code>1:(2:(3:[]))</code>). In this case <code>1</code> is the head, and <code>[2,3]</code> is the tail. We can also have <code>3:[]</code>, so <code>3</code> is the head, and the empty list <code>[]</code> is the tail.</p>
<h2 id="recursion">Recursion</h2>
<p>So how do we put our knowledge of lists together to work out the length of a list. We saw that lists themselves are defined <a href="https://www.google.com.au/search?q=recursion">recursively</a> (a list is defined in terms of itself), and we can use that same approach to define a list&#8217;s length.</p>
<p>Let&#8217;s take another look at our current version of <code>length'</code>:</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; [] = 0
length&#39; [a] = 1
length&#39; [b,a] = 2</code></pre>
<p>We can rewrite the last line in the head / tail pattern:</p>
<pre><code>length&#39; (b:[a]) = 2</code></pre>
<p>The second last line says that <code>length' [a] = 1</code>, so combining those two lines gives us:</p>
<pre><code>length&#39; (b:[a]) = 1 + length&#39; [a]</code></pre>
<p>Here we&#8217;ve defined the length of a two element list as one plus the length of a one element list. We can generalise this to any list: the length of a list is one plus the length of the tail of the list. Using this fact, we can finally get a working version of our function:</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; [] = 0
length&#39; (head:tail) = 1 + length&#39; tail</code></pre>
<p>Let&#8217;s trace through an example:</p>
<pre><code>length&#39; [1,2,3]
  = length&#39; 1:[2,3]
  = 1 + length&#39; 2:[3]
  = 1 + 1 + length&#39; 3:[]
  = 1 + 1 + 1 + length&#39; []
  = 1 + 1 + 1 + 0
  = 3</code></pre>
<p>Our <code>length'</code> function keeps calling itself until it gets to <code>length' []</code>, which equals <code>0</code>. This is called the <em>stopping case</em> (also known as the <em>base</em> or <em>terminating</em> case), which does not make any further recursive calls and so causes the chain of function calls stop. And now we can handle all kinds of lists:</p>
<pre><code>ghci&gt; length&#39; []
0
ghci&gt; length&#39; [1,2,3,4,5]
5
ghci&gt; length&#39; [1..2001]
2001</code></pre>
<h2 id="the-end-of-the-beginning">The end of the beginning</h2>
<p>The current version of our function is fine, and we&#8217;d be quite entitled to stop here. But we can take this example quite a bit further to help us understand more about Haskell, and also to give us a glimpse into more idiomatic Haskell style. I think I&#8217;m right in saying that it would be quite rare for an experienced Haskeller to write a function like this (I&#8217;m a novice still, so I can&#8217;t say for sure). So let&#8217;s press on!</p>
<h2 id="folds">Folds</h2>
<p>The recursive pattern we&#8217;ve defined for <code>length'</code> crops up all over the place when dealing with lists. It&#8217;s so common that this way of traversing a list has been <a href="http://davesquared.net/categories/-folds">abstracted into dedicated functions, called <em>folds</em></a>. Folds reduce the amount of code we need to write, have mathematical properties that can help us prove things about our code, and provide instantly recognisable patterns of recursion. At first I found folds much harder to read than the equivalent explicit recursive code, but after a bit of practice I find them as least as clear as their explicit or imperative equivalents, and generally prefer them to long-hand recursion.</p>
<p>To work out the length of our list we&#8217;re going to have to traverse every element in our list, which means our function is never going to work on an infinite list. This is a clue that we want a left fold (to work on infinite lists we have to use a right fold. For everything else we&#8217;ll pretty much use a left fold).</p>
<div class="note">
<strong>Aside:</strong> How can we work with infinite lists? One example is mapping a function over an infinite list, then only taking the first 10 elements. In Haskell this works fine. But getting the length of an infinite list then taking a piece of the answer is never going to work.
</div>

<p>I&#8217;ve <a href="http://davesquared.net/categories/-folds">cataloged my attempts to understand folds in excruciating detail</a>, so take a look at that if you&#8217;re keen to see how explicit recursion translates to folds, but for now let&#8217;s write an imperative version of left fold using C# to illustrate how it works:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="n">TResult</span> <span class="n">FoldLeft</span><span class="p">&lt;</span><span class="n">TItem</span><span class="p">,</span> <span class="n">TResult</span><span class="p">&gt;(</span>
</span><span class='line'>    <span class="n">Func</span><span class="p">&lt;</span><span class="n">TResult</span><span class="p">,</span> <span class="n">TItem</span><span class="p">,</span> <span class="n">TResult</span><span class="p">&gt;</span> <span class="n">combine</span><span class="p">,</span>
</span><span class='line'>    <span class="n">TResult</span> <span class="n">initialValue</span><span class="p">,</span>
</span><span class='line'>    <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">TItem</span><span class="p">&gt;</span> <span class="n">items</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="kt">var</span> <span class="n">accumulator</span> <span class="p">=</span> <span class="n">initialValue</span><span class="p">;</span>
</span><span class='line'>    <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">item</span> <span class="k">in</span> <span class="n">items</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">accumulator</span> <span class="p">=</span> <span class="n">combine</span><span class="p">(</span><span class="n">accumulator</span><span class="p">,</span> <span class="n">item</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">accumulator</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>The first argument is itself a function that combines a list item with the value accumulated during the previous calls. The second argument gives the case for the empty list (the stopping case for the recursion). Finally, we return the accumulated result.</p>
<p>Here&#8217;s our rewritten function:</p>
<pre><code>import Data.List

length&#39; :: [a] -&gt; Int
length&#39; list = foldl&#39; onePlusLengthOfTail 0 list
    where onePlusLengthOfTail tailLength listElement = 1 + tailLength</code></pre>
<p>Despite looking completely different, this works exactly the same as our previous <code>length'</code> function. Let&#8217;s look at some key features.</p>
<p>First, we&#8217;ve added <code>import Data.List</code> so that we can use the <code>foldl'</code> (fold left) function.<sup><a href="#fn2" class="footnoteRef" id="fnref2">2</a></sup> We&#8217;ll omit this line from later code samples, but be aware when you see <code>foldl'</code> you&#8217;ll need to have the correct <a href="http://www.haskell.org/haskellwiki/Import">import</a> in the source file.</p>
<p>Next, we now only need the one input pattern, as our fold will take care of the empty list case and the head/tail recursion stuff. The empty list case returns <code>0</code>, and you can see that is the second parameter to <code>foldl'</code>.</p>
<p>We&#8217;re also using a <code>where</code> clause. This defines a function called <code>onePlusLengthOfTail</code>, but it can only be called from our <code>length'</code> function. It is like a little private function for the sole use of its parent function. <code>onePlusLengthOfTail</code> combines a list element with the accumulated result of the recursion. In this case we&#8217;re saying that for each list element we want to add one plus whatever the result was of recursively calling the previous calls, which is exactly what our previous implementation did.</p>
<h2 id="lambdas">Lambdas</h2>
<p>Pulling out helper functions like <code>onePlusLengthOfTail</code> is often helpful, but in some cases it adds little but noise. Often with Haskell the types will often tell us more information than names do, so once we&#8217;re familiar with the fold syntax naming the combining function will typically give us little benefit.</p>
<p>Instead, we can include the function in-line using lambda notation. Haskell&#8217;s syntax for lambdas is quite similar to C#&#8217;s. In C# we&#8217;d write <code>x =&gt; x + 1</code>, in Haskell <code>\x -&gt; x + 1</code>.</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; list = foldl&#39; (\acc x -&gt; 1+acc) 0 list</code></pre>
<p>The <code>\</code> symbol was chosen for its slight resemblance to the lambda symbol &#8216;λ&#8217;. After that are the function arguments, which I&#8217;ve called <code>acc</code> for accumulator (it is the accumulated value of our recursion) and <code>x</code> for whatever list element we&#8217;re looking at. Now we don&#8217;t actually use the value of <code>x</code>, so Haskellers would generally replace it with an underscore like this: <code>\acc _ -&gt; 1+acc</code>. This isn&#8217;t actually an argument name, it is Haskell&#8217;s symbol for a catch-all pattern. It will match anything, a bit like the <code>*</code> wildcard character typically used for searches.</p>
<h2 id="partial-function-application">Partial function application</h2>
<p>Remember our <code>const'</code> function which takes two arguments, ignores the second and returns the first? What happens when we call it with one argument?</p>
<p>Let&#8217;s use GHCi&#8217;s <code>:t</code> command to check it&#8217;s type:</p>
<pre><code>ghci&gt; :t const&#39;
const&#39; :: a -&gt; b -&gt; a
ghci&gt; :t const&#39; &#39;a&#39;
const&#39; &#39;a&#39; :: b -&gt; Char</code></pre>
<p>Normally <code>const'</code> is type <code>a -&gt; b -&gt; a</code>, but <code>const' 'a'</code> is type <code>b -&gt; Char</code>. What&#8217;s going on?</p>
<p>Well, all functions in Haskell are <em>curried</em>, which means they only take one argument, and return a function that takes the rest. So the real type of <code>const'</code> is actually <code>a -&gt; (b -&gt; a)</code>; it takes an <code>a</code> and returns a function which takes a <code>b</code> and returns an <code>a</code>. Because of the <a href="http://davesquared.net/2012/04/associativity.html">associativity</a> of the <code>-&gt;</code> operator and of function application, we can write it either way. This also means we can call Haskell functions with as many or as few of the required arguments as we like, and we&#8217;ll get back a function to finish the job as soon as we provide the remainder of the arguments.</p>
<p>This is known as partial application. We can partially apply the arguments to a function to get back another function. Once all the arguments have been passed in we&#8217;ll get the final value.</p>
<p>The best example I can think of to illustrate this is the <code>(+)</code> function. Normally this would take 2 arguments (such as <code>4 + 1</code>, or <code>(+) 4 1</code>, both are legal in Haskell. The former is called <em>infix</em> position, and the latter is <em>function notation</em>.) Let&#8217;s just apply one argument to <code>(+)</code>:</p>
<pre><code>ghci&gt; let addOne = (1+)
ghci&gt; :t addOne
addOne :: Integer -&gt; Integer
ghci&gt; addOne 4
5
ghci&gt; addOne 72
73</code></pre>
<p>Here we&#8217;ve partially applied the argument <code>1</code> to <code>(+)</code>, which has returned a function that takes the next argument and returns the result. We can then call <code>addOne</code> as a single argument function.</p>
<h2 id="back-on-topic-please-dave">Back on topic please Dave!</h2>
<p>But we are still on topic! Look at our <code>length'</code> function:</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; list = foldl&#39; (\acc _ -&gt; 1+acc) 0 list</code></pre>
<p>It takes a <code>[a]</code> and returns an <code>Int</code>. Now the <code>foldl'</code> on the right hand side takes a function, an <code>Int</code> and a <code>[a]</code> and returns an <code>Int</code>. If we think back to partial application, that means that <code>foldl' (\acc _ -&gt; 1+acc)</code> takes an <code>Int</code> and a <code>[a]</code> and returns an <code>Int</code>. Which means that <code>foldl' (\acc _ -&gt; 1+acc) 0</code> takes a <code>[a]</code> and returns an <code>Int</code>.</p>
<p>Hold on, read the last sentence again! Both <code>length'</code> and <code>foldl' (\acc _ -&gt; 1+acc) 0</code> are of type <code>[a] -&gt; Int</code>. So really:</p>
<pre><code>length&#39; = foldl&#39; (\acc _ -&gt; 1+acc) 0 </code></pre>
<p>We don&#8217;t need to explicitly add the <code>list</code> argument, as <code>foldl' (\acc _ -&gt; 1+acc) 0</code> is going to return a function which takes a list anyway. Just as we were able to write <code>let addOne = (1+)</code>, we can assign the name <code>length'</code> to <code>foldl' (\acc _ -&gt; 1+acc) 0</code>.</p>
<p>The general rule is whenever an argument appears on the end of both the left and right hand sides of our function definition, we can drop it off thanks to partial application. (The one catch is when the last argument is surrounded by parentheses such as <code>f (g x)</code>. We need to change that a little before we can drop off the <code>x</code>. We&#8217;ll see how to do that shortly.)</p>
<p>This may initially seem a little pointless, but we&#8217;ll rarely see Haskell code that keeps an unrequired argument on both sides so it is a good idea to get familiar with it. Once we get comfortable with partial application keeping the arguments just appears wasteful, and will stick out like a sore thumb.</p>
<h2 id="point-free">Point free</h2>
<p>Not explicitly referencing arguments is called <a href="http://buffered.io/posts/point-free-style-what-is-it-good-for">point-free style</a>. While the particular case mentioned above (dropping an argument referenced on both sides of a function definition) is pretty essential for idiomatic Haskell, there is a little more debate over point-free style, where every effort is made to not reference any arguments at all. (Hence why it is also known, mainly jokingly, as &quot;point-less&quot; style :).)</p>
<p>Much like folds, I initially couldn&#8217;t see why anyone would want to write in point-free style as it seems to make code so much more difficult to read. Again, after a little time adjusting to it, I found that I could start to read it quite naturally, with the added benefit of being concise, and making the all-important composition and chaining of functions much neater. That said, I&#8217;d guess that even the most ardent point-free fans would acknowledge that there are some cases where having explicit arguments is clearer, so the aim is not to dogmatically pursue point-free functions, but to keep an eye out for where heading in that direction will help our code. Love it or hate it, I&#8217;d suggest getting comfortable with reading point-free style as you&#8217;ll see lots of Haskell code written like this, and it is one of those things you need to give yourself a chance to get used to.</p>
<p>To move a bit closer to a point-free function, let&#8217;s get rid of the reference to the unused second argument of our lambda.</p>
<pre><code>(\acc _ -&gt; 1+acc)</code></pre>
<p>This is a function which takes an <code>Int</code> and an <code>a</code>, ignores that <code>a</code> and returns a new <code>Int</code>. Ignores the second argument huh? That sounds a lot like our <code>const'</code> function:</p>
<pre><code>(\acc x -&gt; const&#39; (1+acc) x)</code></pre>
<p>We&#8217;ve had to put the <code>x</code> back in for now so we can reference it on the right hand side of our function definition, but now we have the <code>x</code> as the last argument on both sides. And from our work with partial application we know that means we can drop it:</p>
<pre><code>(\acc -&gt; const&#39; (1+acc))</code></pre>
<p>Now we&#8217;re partially applying one argument to <code>const'</code>, so it will return a function which takes another argument, ignores it and return the first argument it was initially called with (<code>1+acc</code>).</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; = foldl&#39; (\acc -&gt; const&#39; (1+acc)) 0 </code></pre>
<h2 id="function-composition">Function composition</h2>
<p><a href="http://en.wikipedia.org/wiki/Function_composition_(computer_science)">Function composition</a> is a way of combining functions that each take one argument. Say we have two functions, <code>f :: b -&gt; c</code> and <code>g :: a -&gt; b</code>. We can compose <code>f</code> and <code>g</code> such that:</p>
<pre><code>(f . g) x = f (g x)</code></pre>
<p>The <code>(.)</code> operator means composition. Notice that <code>g x</code> returns a <code>b</code>, and <code>f</code> takes a <code>b</code> and returns a <code>c</code>. So <code>f . g :: a -&gt; c</code>. Again, initially confusing, ultimately awesome. Composition means we can wire up lovely pipelines of functions that values flow through, without the traditional imperative steps of call this, assign to that, call another, assign to new variable and so on.</p>
<p>Now we&#8217;ve now got this little <code>const' (1+acc)</code> expression which is just pleading with us to use function composition, but it&#8217;s a little hard to see at the moment. It would be clearer if we used our <code>addOne</code> function from earlier. Remember we wrote <code>let addOne = (1+)</code>? Well in Haskell this function already exists, and is called <code>succ</code> for <em>successor</em>.</p>
<pre><code>const&#39; (succ acc)</code></pre>
<p>This is now in the form <code>f (g x)</code>, where <code>f = const'</code> and <code>g = succ</code>. So substituting in for <code>f . g</code>:</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; = foldl&#39; (\acc -&gt; (const&#39; . succ) acc) 0 </code></pre>
<p>And look what&#8217;s appeared on both sides of our lambda: <code>\acc -&gt; (const' . succ) acc</code>. Thanks to partial application we can drop off the <code>acc</code> argument and we have:</p>
<pre><code>length&#39; :: [a] -&gt; Int
length&#39; = foldl&#39; (const&#39; . succ) 0 </code></pre>
<p>It&#8217;s small, it&#8217;s beautiful, it actually works, and if you&#8217;re just starting out it&#8217;s probably utterly incomprehensible. Give it a little while though. With a bit of practice it&#8217;ll be second nature to you in next to no time. (I didn&#8217;t believe this either, but after a couple of months I&#8217;ve been suitably warped by the small amount of functional programming I&#8217;ve done that I&#8217;m actually starting to prefer this. :))</p>
<h2 id="wat">Wat?</h2>
<p>While we&#8217;re waiting for our brains to warp enough to be able to read this fluently, let&#8217;s look at some quick tips to help us decipher it.</p>
<p>First, we have our function name <code>length'</code> and the type signature. It takes a list of <code>a</code> and returns an <code>Int</code>. It&#8217;s a pretty safe bet that it is intended to return the length of a list.</p>
<p>Next, we can see it&#8217;s using a fold, which means it is recursing over the list. It has a <code>0</code> as the second parameter to the fold, so that means when given the empty list the length will be <code>0</code>.</p>
<p>Now we have this strange point-free function passed to <code>foldl'</code>. I find it easiest to read composed functions is to go from right to left. Our composed function will call <code>succ</code> on an argument (add one to it), and call <code>const'</code> on the result, which will take two arguments but ignore the second. So we&#8217;re adding one to the first argument passed in. (Another approach is to translate back to <code>acc -&gt; const' (succ acc)</code>, and take as many steps reversing the process we went through to get to the point it becomes clear.)</p>
<p>Finally, we know (or can look up via <a href="http://www.haskell.org/hoogle/">Hoogle</a> or using <code>:t</code> in GHCi) that <code>foldl'</code> calls the <code>const'.succ</code> function with first the accumulated value of the fold, and the current list item. So we&#8217;re going to start with our base case of 0, and for each list item we&#8217;re going to add 1 to it. Which will give us the length of the list.</p>
<h2 id="learning-cliff">Learning cliff</h2>
<p>I found these topics quite confusing when I first encountered them. If you&#8217;re feeling similarly, it&#8217;s worth keeping in mind that most of us have been trained to think of programming imperatively rather than functionally, so it is naturally going to take some time to adjust to ideas like reducing expressions, folds, function composition and point-free style. I really believe it is worth persisting with. Even if you don&#8217;t end up using it regularly, it is worth it to get a different way of looking at programming problems.</p>
<h2 id="conclusion">Conclusion</h2>
<p>In this post we&#8217;ve seen some of the basics of Haskell&#8217;s type system and function syntax, and applied pattern matching on lists and recursion to implement our own <code>length'</code> function.</p>
<p>We&#8217;ve then seen how to replace the explicit recursion code with a fold, before applying partial application and function composition to make the code very concise.</p>
<p>These techniques are very powerful (more so than this simple example suggests), and by learning to use them we can get real, safe reuse of code by partially applying and composing small, very cohesive functions in a myriad of ways. Even in this small example we used <code>foldl'</code>, <code>const</code> and <code>succ</code>; we wrote almost no code, and instead just wired together existing pieces. Coupling to something as fundamental as &quot;adding one&quot; is a much safer kind of reuse than I&#8217;m used to being bitten with in other programming paradigms.</p>
<p>I hope you enjoyed this post. As you can probably tell, I&#8217;m really enjoying learning a different perspective on programming, and I hope I&#8217;ve managed to pass some of that along. Please let me know any parts are unclear so I can try and improve them.</p>
<section class="footnotes">
<hr>
<ol>
<li id="fn1"><p> <a href="http://www.haskell.org/haskellwiki/Pronunciation">Pronounciation guide</a>, HaskellWiki<a href="#fnref1">↩</a></p></li>
<li id="fn2"><p>Haskell provides a <code>foldl</code> function in the default module it loads, but in practice the <code>foldl'</code> variant is almost always preferred for left folds as it is more space efficient and can prevent stack overflows when working with large lists. See <a href="http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl&#39;">Foldr Foldl Foldl&#8217;</a> on HaskellWiki for a detailed explanation.<a href="#fnref2">↩</a></p></li>
</ol>
</section>]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[FP newbie learns a little about functors]]></title>
    <link href="http://davesquared.net/2012/05/fp-newbie-learns-functors.html"/>
    <updated>2012-05-03T23:40:00+10:00</updated>
    <id>http://davesquared.net/2012/05/fp-newbie-learns-functors</id>
    <content type="html"><![CDATA[<p>As you may have noticed I&#8217;m a little obsessed with <a href="http://davesquared.net/categories/functional-programming/">functional programming</a> at the moment. I&#8217;ve recently been reading up on bizarre-sounding terms like <a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids">functors, applicatives and monoids in Learn You A Haskell</a>, only to discover they&#8217;re not nearly as incomprehensible as they sound. This post outlines some of the information on functors that this newbie has been able to pick up so far.</p>
<!-- more -->

<div class="note">
<strong>Note:</strong> One thing I&#8217;ve noticed while reading introductory FP material is that each concept seems quite small and understandable, but happens to depend on another million or so small and understandable things. I find this makes it difficult to write about without assuming lots of knowledge, going down a rabbit hole of detail, or glossing over potentially important points. For this post I&#8217;ve elected to gloss over details like type constructors and type classes in favour of trying to give a general overview. If you&#8217;d like to get the details instead, skip through to the <a href="#references">References</a>, starting with <a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids">Learn You A Haskell</a>.
</div>

<h2 id="types-containing-values">Types containing values</h2>
<p>Many types can be thought of as holding values. For example, lists and arrays can hold integers, strings or other types of values. In .NET, we have the <code>Nullable&lt;T&gt;</code> type, which can hold <code>null</code>, or a value of type <code>T</code>. In Haskell there&#8217;s the <code>Maybe a</code> type, which similarly can hold <code>Nothing</code> or an <code>a</code>. We can even think of commands or partially evaluated functions as holding values: once they finish executing they&#8217;ll have or return a particular value.</p>
<p>For now, let&#8217;s just think of these types as boxes or containers that can hold values of some type.</p>
<h2 id="mapping-over-boxes-of-values">Mapping over boxes of values</h2>
<p>Say we have a function <code>fn</code> which can take some value of type <code>a</code> and return a value of type <code>b</code>. In Haskell we express this as <code>fn :: a -&gt; b</code>, or as <code>Func&lt;A,B&gt; fn</code> in C#.</p>
<figure>
<img src="http://davesquared.net/images/2012/function_a_to_b.png" alt="An &#39;a&#39; is passed into &#39;fn&#39;, and it spits out a &#39;b&#39;"><figcaption>An &#8216;a&#8217; is passed into &#8216;fn&#8217;, and it spits out a &#8216;b&#8217;</figcaption>
</figure>
<p>Now imagine we have some sort of box that can hold some quantity of <code>a</code>. We&#8217;d like to apply <code>fn</code> to the <code>a</code> we have in the box, and get a new box of <code>b</code>. But <code>fn</code> works on an <code>a</code>, not on a &quot;box of <code>a</code>&quot;, so we need a way of apply our <code>fn</code> inside the box, and then a way of shoving the resulting <code>b</code> into a new box.</p>
<figure>
<img src="http://davesquared.net/images/2012/fmap.png" alt="&#39;a&#39; is passed from the box into &#39;fn&#39;, and the &#39;b&#39; is returned in a new box"><figcaption>&#8216;a&#8217; is passed from the box into &#8216;fn&#8217;, and the &#8216;b&#8217; is returned in a new box</figcaption>
</figure>
<p>The solution is to define a function called <code>fmap</code> which does exactly that. It takes a function <code>(a -&gt; b)</code>, and a box of <code>a</code>, and returns a box of <code>b</code>:</p>
<pre><code>-- Pseudo-Haskell
fmap :: (a -&gt; b) -&gt; boxOf a -&gt; boxOf b

// Pseudo-C#
Box&lt;B&gt; Fmap&lt;A,B&gt;(Func&lt;A,B&gt; fn, Box&lt;A&gt; boxOfA);</code></pre>
<h2 id="defining-fmap">Defining fmap</h2>
<p>Now we&#8217;ve previously mentioned that these boxes can actually be different types (lists, <code>Maybe</code>s or <code>Nullable&lt;T&gt;</code>s). How does <code>fmap</code> know how to unpack and repack all the different types of boxes?</p>
<p>To be able to do this, each type of box needs its own implementation of <code>fmap</code>. Boxes that have a valid <code>fmap</code> defined for them are called <em>functors</em>. Haskell has a lot of these built-in, but we could also define them ourselves:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="c1">-- For lists:</span>
</span><span class='line'><span class="nf">fmap</span> <span class="n">fn</span> <span class="n">list</span> <span class="ow">=</span> <span class="n">map</span> <span class="n">list</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- For Maybe, which can be either Nothing or (Just someValue)</span>
</span><span class='line'><span class="nf">fmap</span> <span class="n">fn</span> <span class="kt">Nothing</span> <span class="ow">=</span> <span class="kt">Nothing</span>
</span><span class='line'><span class="nf">fmap</span> <span class="n">fn</span> <span class="p">(</span><span class="kt">Just</span> <span class="n">someValue</span><span class="p">)</span> <span class="ow">=</span> <span class="kt">Just</span> <span class="p">(</span><span class="n">fn</span> <span class="n">someValue</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>Notice that <code>fmap</code> for a list is exactly the same as normal <code>map</code>. In other words, we iterate over each value in the list, apply the function to each, and then return the new list. For <code>Maybe</code>, mapping the function over <code>Nothing</code> returns <code>Nothing</code>. Mapping over an instance that holds a value gets that value, applies the function to it, and returns a new instance of <code>Just</code> the result.</p>
<p>C#&#8217;s type system doesn&#8217;t support all the features we need to get a lot of use out of functors, but we can still get an idea of how <code>fmap</code> might look for enumerables and nullables:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">B</span><span class="p">&gt;</span> <span class="n">fmap</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="p">&gt;(</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">&gt;</span> <span class="n">fn</span><span class="p">,</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">A</span><span class="p">&gt;</span> <span class="n">functor</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">functor</span><span class="p">.</span><span class="n">Select</span><span class="p">(</span><span class="n">fn</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="n">B</span><span class="p">?</span> <span class="n">fmap</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">&gt;(</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">&gt;</span> <span class="n">fn</span><span class="p">,</span> <span class="n">A</span><span class="p">?</span> <span class="n">functor</span><span class="p">)</span> <span class="k">where</span> <span class="n">A</span> <span class="p">:</span> <span class="k">struct</span> <span class="nc">where</span> <span class="n">B</span> <span class="p">:</span> <span class="k">struct</span> <span class="err">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">functor</span> <span class="p">==</span> <span class="k">null</span> <span class="p">?</span> <span class="k">null</span> <span class="p">:</span> <span class="p">(</span><span class="n">B</span><span class="p">?)</span> <span class="n">fn</span><span class="p">(</span><span class="n">functor</span><span class="p">.</span><span class="n">Value</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<h2 id="graduating-from-boxes">Graduating from boxes</h2>
<p>Notice that <code>fmap</code> preserves the type of box used. For example, if we call <code>fmap</code> on a list, we&#8217;ll get another list back. If we call <code>fmap</code> on a <code>Maybe</code>, we&#8217;ll get another <code>Maybe</code> back. We never call it with a list and get a <code>Maybe</code> back. If we call our box <code>f</code>, then calling <code>fmap</code> on <code>f a</code> will give us back an <code>f b</code>. The <code>f</code> is preserved.</p>
<p>Rather than using the box analogy, the type <code>f</code> is often referred to as the <em>context</em>, so <code>f a</code> is an <code>a</code> value in the context of <code>f</code>. An <code>a</code> in a context where there maybe be 0 or more values is a list of <code>a</code>. <code>Maybe</code> is a context that may have zero or one <code>a</code>. <code>fmap</code> maps a function from <code>a</code> to <code>b</code>, preserving the context.</p>
<h2 id="lifting-functions-is-useful">Lifting functions is useful</h2>
<p>Haskell (and I assume most functional programming languages) has some really interesting ways of putting types together. It is quite common to have simple data types (like integers, strings etc) and functions that operate on them. We can also have more complex data types that can work over many types (they are <em>polymorphic</em>), such as lists of <code>a</code> or <code>Maybe a</code> (where <code>a</code> can be an integer, string, function or even another type of list). Because this polymorphism is so common, it would be nice to be able to reuse functions that work on simple types in different contexts, and that&#8217;s exactly what functors let us do.</p>
<p>This is known as <em>lifting</em> a function; transforming a function like <code>a -&gt; b</code> to work in another context, like <code>f a -&gt; f b</code>.</p>
<p>For example, we can use lift a standard integer operation like (+1) to work on <code>Maybe Int</code>:</p>
<pre><code>ghci&gt; fmap (+1) (Just 10)
Just 11
ghci&gt; fmap (+1) Nothing
Nothing</code></pre>
<p>Or apply a standard string function to a <code>Maybe String</code>:</p>
<pre><code>ghci&gt; fmap reverse (Just &quot;hello world!&quot;)
Just &quot;!dlrow olleh&quot;
ghci&gt; fmap reverse Nothing
Nothing</code></pre>
<p>And then of course there is the familiar mapping over a list (as mentioned earlier, <code>map</code>, or <code>Select</code> in .NET, is just <code>fmap</code> for list):</p>
<pre><code>ghci&gt; fmap (*2) [1..10]
[2,4,6,8,10,12,14,16,18,20]

csharp&gt; new[] {1,2,3,4,5,6,7,8,9,10}.Select(x =&gt; x*2);
{ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }</code></pre>
<p>As a <a href="http://en.wikipedia.org/wiki/Purely_functional">pure functional language</a>, Haskell eschews code with side-effects, which includes some innocuous-sounding things like reading or writing to files. To work within this restriction, side-effects can be wrapped up in types like <code>IO a</code>. Because <code>IO</code> is a functor, we can use <code>fmap</code> to work with the value inside the <code>IO</code> context without having to explicitly pull out the value (so side-effects stay neatly wrapped up in their <code>IO</code> box). Say we wanted to read an integer from STDIN, here&#8217;s how it would work with and without <code>fmap</code>:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'>
<figcaption><span></span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">doubleInput</span> <span class="ow">=</span> <span class="kr">do</span>
</span><span class='line'>    <span class="n">input</span> <span class="ow">&lt;-</span> <span class="n">getLine</span>
</span><span class='line'>    <span class="kr">let</span> <span class="n">enteredNumber</span> <span class="ow">=</span> <span class="n">read</span> <span class="n">input</span> <span class="ow">::</span> <span class="kt">Int</span>   <span class="c1">-- read string as int</span>
</span><span class='line'>    <span class="kr">let</span> <span class="n">double</span> <span class="ow">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">enteredNumber</span>          <span class="c1">-- (2*)</span>
</span><span class='line'>    <span class="n">return</span> <span class="o">$</span> <span class="n">show</span> <span class="n">double</span>                    <span class="c1">-- show int as string</span>
</span><span class='line'>
</span><span class='line'><span class="nf">doubleInputWithFmap</span> <span class="ow">=</span> <span class="n">fmap</span> <span class="p">(</span><span class="n">show</span> <span class="o">.</span> <span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="p">)</span><span class="o">.</span> <span class="n">read</span><span class="p">)</span> <span class="n">getLine</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>Finally, functors are just one part of a <a href="http://www.haskell.org/haskellwiki/Image:Typeclassopedia-diagram.png">large hierarchy of type classes</a>. Other type classes like applicative functors and monads build on the properties of functors to provide their own interesting capabilities.</p>
<h2 id="well-behaved-functors">Well-behaved functors</h2>
<p>We&#8217;ve seen that functors are types that can can have functions mapped over them, thanks to an corresponding implementation of <code>fmap</code>. That&#8217;s the bulk of it, but there are also two formal properties that these <code>fmap</code> implementations need to have in order for a type to act as a functor.</p>
<p>The first is that mapping the identity function over a functor is the same as calling the identity function directly on the functor. <code>id</code> is the identity function in Haskell, the C# equivalent being <code>x =&gt; x</code>. The identity function takes an argument and returns it, unchanged.</p>
<pre><code>fmap id a = id a

-- Example
ghci&gt; id (Just 42)
Just 42
ghci&gt; fmap id (Just 42)
Just 42</code></pre>
<p>The second is that composing two functions then mapping them needs to give the same result as mapping each function in turn. In other words:</p>
<pre><code>fmap (f . g) = fmap f . fmap g</code></pre>
<p><a href="http://www.haskell.org/haskellwiki/Typeclassopedia#Laws">Typeclassopedia&#8217;s entry on functor laws</a> mentions that satisfying the first law automatically satisfies the second. Which is good, because to me the first is a lot easier to follow. :)</p>
<p>These properties aren&#8217;t enforced by the type system, but we need to make sure our functors have them or else our functor won&#8217;t behave like all the other nice functors. These properties guarantee that <code>fmap</code> preserves the structure (or context) of our functor.</p>
<h2 id="references">References</h2>
<ul>
<li><a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids">&quot;Functors, Applicative Functors and Monoids&quot;</a>, Learn You A Haskell</li>
<li><a href="http://en.wikipedia.org/wiki/Functor">&quot;Functor&quot;</a>, Wikipedia</li>
<li><a href="http://en.wikipedia.org/wiki/Map_(higher-order_function)#Generalization">&quot;Map (higher-order function)&quot;</a>, Wikipedia</li>
<li><a href="http://www.haskell.org/haskellwiki/Typeclassopedia">&quot;Typeclassopedia&quot;</a>, HaskellWiki</li>
<li><a href="http://en.wikibooks.org/wiki/Haskell/Applicative_Functors#Functors">&quot;Applicative functors&quot;</a>, Haskell Wikibook</li>
</ul>]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Design via minimum code]]></title>
    <link href="http://davesquared.net/2012/05/design-via-minimum-code.html"/>
    <updated>2012-05-01T22:10:00+10:00</updated>
    <id>http://davesquared.net/2012/05/design-via-minimum-code</id>
    <content type="html"><![CDATA[<p>Being a bit of a <a href="http://en.wikipedia.org/wiki/Pessimism">realist</a> (;)), I find it much easier to find problems with my design ideas than I do finding designs I&#8217;m really happy with. I am always cognisant of this and make sure to balance the needs of doing a good job and getting the job done. One technique I find useful for this is thinking about the minimum amount of code required.</p>
<p>Whenever I find myself evaluating different design patterns or counting responsibilities to work out where the feature should go or which areas I should refactor, I stop and work out the minimum amount of code I&#8217;d need to write to solve the problem, ignoring design considerations. Once I&#8217;ve starting thinking about the problem in that light, working out where to put that code becomes a bit easier. If the code violates my sense of design aesthetics, it could just be because it is a messy problem, and no matter how hard I try to abstract or design my way out, the fundamental messiness still remains.</p>
<!-- more -->

<p>As an example, say we&#8217;re working with a library that misuses exceptions, throwing for genuine exceptional cases, and also as a way to notify callers of a change in mode. I might find myself getting grand ideas like anti-corruption layers, Command patterns with reused error handling, or trying various continuation ideas. If there&#8217;s a lot of existing code I might start getting bogged down in working out all the classes I&#8217;m going to have to change to fit in with this new error handling infrastructure. These kind of premature generalisations are a sure sign I need to stop and simplify.</p>
<p>If we think about the problem for the moment, at a minimum we&#8217;re going to need a <code>try .. catch</code> somewhere, and chances are we&#8217;re going to have to do something disgusting like swallow the irrelevant exceptions. We&#8217;re now in a position to think about exactly where this responsibility should go based on our standard design rules. In this case, it may be best to deal with this immediately at the boundary between our code and the library, rather than creating a more formal abstraction for handling them (or not, the point is that we know the code and minimum mess required, so we are in a better position to work out where to put it).</p>
<div class="note">
<strong>Note:</strong> I&#8217;d like to be sure to quickly distinguish this from <a href="http://en.wikipedia.org/wiki/YAGNI">YAGNI</a>, which in my experience is a decent idea frequently misused for copping out. Once we&#8217;ve determined the minimum amount of code required, if doing a bit of work to get that into a nice shape is needed then calling faux-YAGNI is definitely inadvisable.
</div>

<p>TDD can be a useful way to do a similar thing; writing a test that uncovers the minimum increment of code required. Sometimes though it&#8217;s not clear from what class or in which direction you should drive the code, in which case I&#8217;ve found thinking in terms of the minimum amount of code required then spiking some options around that a helpful alternative.</p>
<p>So next time you&#8217;re pondering whether to toggle a UI element&#8217;s visibility using MVVM or an MVP-variant for UI separation, or whether to push the conditional in to a Strategy implementation, just remember that somewhere along the line an evil <code>if</code> statement or similar is going to have to execute and switch the element&#8217;s visibility switched from &quot;visible&quot; to &quot;hidden&quot;.</p>]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Working out function types: map map]]></title>
    <link href="http://davesquared.net/2012/04/map-map.html"/>
    <updated>2012-04-16T18:25:00+10:00</updated>
    <id>http://davesquared.net/2012/04/map-map</id>
    <content type="html"><![CDATA[<p>One of the exercises in <a href="http://www.amazon.com/Introduction-Functional-Programming-International-Computing/dp/0134841972/">Introduction to Functional Programming</a> by Richard Bird and Philip Wadler is to work out the type signature of <code>map map</code> (i.e. calling <code>map</code> with <code>map</code> as its first argument). I&#8217;ve generally struggled to deal with all but the simplest of partial function application, but I found a great thread on the <a href="http://lambda-the-ultimate.org/node/2948">Lambda the Ultimate forums</a> that really helped me out with this. Two commenters suggested different approaches: <a href="http://lambda-the-ultimate.org/node/2948#comment-43448">going through the maths</a>, and <a href="http://lambda-the-ultimate.org/node/2948#comment-43437">understanding the abstraction</a>.</p>
<!-- more -->

<h2 id="solving-algebraically">Solving algebraically</h2>
<p>The signature for <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> is:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mo>:</mo><mo>:</mo><mo stretchy="false">(</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">)</mo><mo>→</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo></mrow></math></p>
<p>The first argument is a function which takes an <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>a</mi></mrow></math> and returns a <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>b</mi></mrow></math>. The second argument is a list of <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>a</mi></mrow></math>. The last type, a list of <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>b</mi></mrow></math>, is the function&#8217;s return value.</p>
<p>To work out the signature of <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math>, we need to substitute in what we know about <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math>&#8217;s type signature to work out the resulting combination of <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>a</mi></mrow></math> and <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>b</mi></mrow></math>. I found this really tricky, but by applying a few rules we can work it out.</p>
<p>First, let&#8217;s pass some arbitrary function <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mo>:</mo><mo>:</mo><mi>c</mi><mo>→</mo><mi>d</mi></mrow></math> as the first argument to <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math>.</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mo>:</mo><mo>:</mo><mo stretchy="false">(</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">)</mo><mo>→</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo></mrow></math> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mo>:</mo><mo>:</mo><mi>c</mi><mo>→</mo><mi>d</mi></mrow></math> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>f</mi><mo>:</mo><mo>:</mo><mo stretchy="false">[</mo><mi>c</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>d</mi><mo stretchy="false">]</mo></mrow></math></p>
<p>In our case, we want to pass <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> as <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi></mrow></math>. But <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi></mrow></math> is a function that takes one argument, while <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> takes two. So how can we use it in place of <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi></mrow></math>? Well, the <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>→</mo></mrow></math> operator is <a href="http://davesquared.net/2012/04/associativity.html">right-associative</a>, which means we can re-write <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> like this:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mo>:</mo><mo>:</mo><mo stretchy="false">(</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">)</mo><mo>→</mo><mo stretchy="false">(</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">)</mo></mrow></math></p>
<p>This is know as <em>currying</em>. In Haskell, all functions can be considered to take one argument, and return a function that takes the remainder of the arguments. Here, we&#8217;re thinking of <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> as a function which takes a function from <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>a</mi><mo>→</mo><mi>b</mi></mrow></math>, and returns a function that takes a <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo></mrow></math> and returns a <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo></mrow></math>.</p>
<p>Now we have <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> in a form that matches <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mo>:</mo><mo>:</mo><mi>c</mi><mo>→</mo><mi>d</mi></mrow></math>:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mo>:</mo><mo>:</mo><mi>c</mi><mo>→</mo><mi>d</mi></mrow></math> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mo>:</mo><mo>:</mo><mo stretchy="false">(</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">)</mo><mo>→</mo><mo stretchy="false">(</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">)</mo></mrow></math></p>
<p>So: <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>c</mi><mo>:</mo><mo>:</mo><mo stretchy="false">(</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">)</mo></mrow></math> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>d</mi><mo>:</mo><mo>:</mo><mo stretchy="false">(</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">)</mo></mrow></math></p>
<p>Now we can substitute back into our <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>f</mi></mrow></math> type declaration:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>f</mi><mo>:</mo><mo>:</mo><mo stretchy="false">[</mo><mi>c</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>d</mi><mo stretchy="false">]</mo></mrow></math> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>m</mi><mi>a</mi><mi>p</mi><mo>:</mo><mo>:</mo><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">)</mo><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">)</mo><mo stretchy="false">]</mo></mrow></math> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>m</mi><mi>a</mi><mi>p</mi><mo>:</mo><mo>:</mo><mo stretchy="false">[</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">]</mo></mrow></math></p>
<p>So <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> takes a list of functions of the form <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">(</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">)</mo></mrow></math> and returns a list of functions that take <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo></mrow></math> an returns <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo></mrow></math> (i.e. <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">(</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">)</mo></mrow></math>).</p>
<h2 id="solving-intuitively">Solving intuitively</h2>
<p>The <a href="http://lambda-the-ultimate.org/node/2948#comment-43437">other suggested approach</a> was to think about the abstractions being used, rather than the mathematical basis for the functions. I tend to struggle with this, but it is worth trying to come to grips with the concepts and intention, and not just rely on somewhat-blindly applying maths principles.</p>
<p>The <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> function conceptually represents transforming a function to work on a list. For example, <code>(+1)</code> adds one to a single number, while <code>map (+1)</code> adds on to a list of numbers. This corresponds to the step in our last approach where we starting thinking of <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> as <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mo>:</mo><mo>:</mo><mo stretchy="false">(</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">)</mo><mo>→</mo><mo stretchy="false">(</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">)</mo></mrow></math>.</p>
<p>Continuing this line of thinking, <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>m</mi><mi>a</mi><mi>p</mi></mrow></math> will then take a list of functions <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">[</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">]</mo></mrow></math> and return a list of transformed functions that work on lists of <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>a</mi></mrow></math>, or <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">[</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">]</mo></mrow></math>. This gives us the same result as last time, <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>m</mi><mi>a</mi><mi>p</mi><mspace width="0.278em"></mspace><mi>m</mi><mi>a</mi><mi>p</mi><mo>:</mo><mo>:</mo><mo stretchy="false">[</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mo stretchy="false">[</mo><mi>a</mi><mo stretchy="false">]</mo><mo>→</mo><mo stretchy="false">[</mo><mi>b</mi><mo stretchy="false">]</mo><mo stretchy="false">]</mo></mrow></math>.</p>
<p>I tend to get lost when I try to think about it this way, but hopefully I&#8217;ll start to get the hang of reasoning about functions this way as I get more practice. If not, there is always the option of falling back on the maths.</p>]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Associativity]]></title>
    <link href="http://davesquared.net/2012/04/associativity.html"/>
    <updated>2012-04-16T18:20:00+10:00</updated>
    <id>http://davesquared.net/2012/04/associativity</id>
    <content type="html"><![CDATA[<p>One of the nice things about pure functional programming is that we can use mathematical properties and axioms to reason about, simplify and derive functions. A property that I&#8217;ve seen crop up a few times is <a href="http://en.wikipedia.org/wiki/Associativity">associativity</a>.</p>
<!-- more -->

<h2 id="associativity-property">Associativity property</h2>
<p>Associativity is a property of some binary operators that means the order operators are evaluated does not matter. An operator <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>⊕</mo></mrow></math> is associative if:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>⊕</mo><mi>y</mi><mo stretchy="false">)</mo><mo>⊕</mo><mi>z</mi><mo>=</mo><mi>x</mi><mo>⊕</mo><mo stretchy="false">(</mo><mi>y</mi><mo>⊕</mo><mi>z</mi><mo stretchy="false">)</mo></mrow></math></p>
<p>A familiar example is addition. We can safely write <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mn>1</mn><mo>+</mo><mn>2</mn><mo>+</mo><mn>3</mn></mrow></math> and know we&#8217;ll get the correct answer irrespective of the order in which it is evaluated, as <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">(</mo><mn>1</mn><mo>+</mo><mn>2</mn><mo stretchy="false">)</mo><mo>+</mo><mn>3</mn><mo>=</mo><mn>1</mn><mo>+</mo><mo stretchy="false">(</mo><mn>2</mn><mo>+</mo><mn>3</mn><mo stretchy="false">)</mo></mrow></math>. An example of a non-associative operator is subtraction, as <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">(</mo><mn>1</mn><mo>−</mo><mn>2</mn><mo stretchy="false">)</mo><mo>−</mo><mn>3</mn><mo>≠</mo><mn>1</mn><mo>−</mo><mo stretchy="false">(</mo><mn>2</mn><mo>−</mo><mn>3</mn><mo stretchy="false">)</mo></mrow></math>.</p>
<p>Two operators are said to associate if they can be evaluated in any order:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>⊕</mo><mi>y</mi><mo stretchy="false">)</mo><mo>⊗</mo><mi>z</mi><mo>=</mo><mi>x</mi><mo>⊕</mo><mo stretchy="false">(</mo><mi>y</mi><mo>⊗</mo><mi>z</mi><mo stretchy="false">)</mo></mrow></math></p>
<h2 id="operator-associativity">Operator associativity</h2>
<p>Related to this mathematical property is <a href="http://en.wikipedia.org/wiki/Operator_associativity">operator associativity</a> or <em>fixity</em>, which is essentially an exercise in parenthesis-saving. For non-associative operations where order of evaluation matters, we can define the operator as <em>left-associative</em> or <em>right-associative</em> depending on how we want it to evaluate in the absence of parentheses.</p>
<p>For a left-associative operator:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>x</mi><mo>⊕</mo><mi>y</mi><mo>⊕</mo><mi>z</mi><mo>=</mo><mo stretchy="false">(</mo><mi>x</mi><mo>⊕</mo><mi>y</mi><mo stretchy="false">)</mo><mo>⊕</mo><mi>z</mi></mrow></math></p>
<p>For a right-associative operator:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>x</mi><mo>⊗</mo><mi>y</mi><mo>⊗</mo><mi>z</mi><mo>=</mo><mi>x</mi><mo>⊗</mo><mo stretchy="false">(</mo><mi>y</mi><mo>⊗</mo><mi>z</mi><mo stretchy="false">)</mo></mrow></math></p>
<h2 id="applying-to-programming">Applying to programming</h2>
<p>In terms of functional programming, a binary operator is a two argument function. We tend to refer to operators as functions used in <a href="http://en.wikipedia.org/wiki/Infix_notation">infix</a> position, or between arguments like <code>a `f` b</code>, as opposed to <em>function notation</em>, which is the more familiar <code>f a b</code> arrangement. A function can be associative regardless of the notation used. Our previous <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mn>1</mn><mo>+</mo><mn>2</mn><mo>+</mo><mn>3</mn></mrow></math> example could be rewritten in function notation as <code>(+) ((+) 1 2) 3 = (+) 1 ((+) 2 3)</code>.</p>
<h3 id="monoids">Monoids</h3>
<p>Associative functions seem useful for reasoning about <a href="http://davesquared.net/2012/03/folds-pt3-left-fold-right.html">folds</a>, particularly when part of a <a href="http://en.wikipedia.org/wiki/Monoid">monoid</a>. A monoid is formed by an associative binary function and an <em>identity element</em> <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>a</mi></mrow></math> that exists such that: <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mtable><mtr><mtd columnalign="right"><mi>x</mi><mo>⊕</mo><mi>a</mi><mo>=</mo><mi>x</mi><mo>=</mo><mi>a</mi><mo>⊕</mo><mi>x</mi></mtd></mtr></mtable></mrow></math></p>
<p>Addition is a monoid for integers, as it is associative and has <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mn>0</mn></mrow></math> as an identity value.</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mi>y</mi><mo stretchy="false">)</mo><mo>+</mo><mi>z</mi><mo>=</mo><mi>x</mi><mo>+</mo><mo stretchy="false">(</mo><mi>y</mi><mo>+</mo><mi>z</mi><mo stretchy="false">)</mo></mrow></math> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>x</mi><mo>+</mo><mn>0</mn><mo>=</mo><mi>x</mi><mo>=</mo><mn>0</mn><mo>+</mo><mi>x</mi></mrow></math></p>
<p>When an operator <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>⊕</mo></mrow></math> and value <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>a</mi></mrow></math> form a monoid this can simplify how we think about folds, as:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mi>o</mi><mi>l</mi><mi>d</mi><mi>r</mi><mo>⊕</mo><mi>a</mi><mspace width="0.278em"></mspace><mo stretchy="false">[</mo><mo stretchy="false">]</mo><mo>=</mo><mi>a</mi></mrow></math> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mi>o</mi><mi>l</mi><mi>d</mi><mi>r</mi><mo>⊕</mo><mi>a</mi><mspace width="0.278em"></mspace><mo stretchy="false">[</mo><msub><mi>x</mi><mn>0</mn></msub><mo>,</mo><msub><mi>x</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>,</mo><msub><mi>x</mi><mi>n</mi></msub><mo stretchy="false">]</mo><mo>=</mo><msub><mi>x</mi><mn>0</mn></msub><mo>⊕</mo><msub><mi>x</mi><mn>1</mn></msub><mo>⊕</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>⊕</mo><msub><mi>x</mi><mi>n</mi></msub></mrow></math></p>
<p>Monoids also give us the <em>first duality theorem</em>: for monoids and a finite list <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>x</mi><mi>s</mi></mrow></math>, <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mi>o</mi><mi>l</mi><mi>d</mi><mi>r</mi><mo>⊕</mo><mi>a</mi><mspace width="0.278em"></mspace><mi>x</mi><mi>s</mi><mo>=</mo><mi>f</mi><mi>o</mi><mi>l</mi><mi>d</mi><mi>l</mi><mo>⊕</mo><mi>a</mi><mspace width="0.278em"></mspace><mi>x</mi><mi>s</mi></mrow></math>. This equivalency means we can decide on which fold to use based purely on efficiency when working with monoids and finite lists.</p>
<p>There seems to be much <a href="http://blog.sigfpe.com/2009/01/haskell-monoids-and-their-uses.html">more to monoids in Haskell</a>, but the point I&#8217;m trying to make is that associativity forms the basis for some more advanced and quite useful concepts (functors being another example).</p>
<h3 id="reducing-noise">Reducing noise</h3>
<p>For maths and programming I&#8217;ve always erred on the side of over-specifying parentheses, but as I get more into functional programming I&#8217;ve found learning operator associativity starts to really reduce the noise. One example is <em>function composition</em> using the <code>(.)</code> operator. Because this is an associative operation, we can compose a string of functions without the noise of parentheses: <code>f . g . h</code>.</p>
<h3 id="currying">Currying</h3>
<p>Another important example of noise-reduction is the behaviour of type-mapping operator <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>→</mo></mrow></math>. Type-mapping is right-associative, which means that <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mo>:</mo><mo>:</mo><mi>a</mi><mo>→</mo><mi>b</mi><mo>→</mo><mi>c</mi></mrow></math> is actually equivalent to <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mo>:</mo><mo>:</mo><mi>a</mi><mo>→</mo><mo stretchy="false">(</mo><mi>b</mi><mo>→</mo><mi>c</mi><mo stretchy="false">)</mo></mrow></math>. This is also known as <a href="http://en.wikipedia.org/wiki/Currying">currying</a>. Let&#8217;s take good old integer addition as an example again. Its type is:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>+</mo><mo>:</mo><mo>:</mo><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>g</mi><mi>e</mi><mi>r</mi><mo>→</mo><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>g</mi><mi>e</mi><mi>r</mi><mo>→</mo><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>g</mi><mi>e</mi><mi>r</mi></mrow></math></p>
<p>Normally we&#8217;d call this with two arguments such as <code>1+2</code> and get an integer <code>3</code> back. But <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>+</mo></mrow></math>&#8217;s type signature can also be expressed as:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>+</mo><mo>:</mo><mo>:</mo><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>g</mi><mi>e</mi><mi>r</mi><mo>→</mo><mo stretchy="false">(</mo><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>g</mi><mi>e</mi><mi>r</mi><mo>→</mo><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>g</mi><mi>e</mi><mi>r</mi><mo stretchy="false">)</mo></mrow></math></p>
<p>This means <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>+</mo></mrow></math> can take an integer, and return a function which takes an integer and returns the final result. So in Haskell we can do things like this:</p>
<pre><code>*Main&gt; :type (+)
(+) :: Num a =&gt; a -&gt; a -&gt; a
*Main&gt; let addOne = (1+)
*Main&gt; :type addOne
addOne :: Integer -&gt; Integer
*Main&gt; addOne 2
3</code></pre>
<p>Here we&#8217;ve given <code>(+)</code> just one argument, and the result is an <code>addOne</code> function that will take a single integer and add one to it. The right-associativity of type mapping, combined with the left-associativity of function application, gives us standard calling semantics for the type declaration <code>a -&gt; a -&gt; a</code>, while also letting us partially apply functions as <code>a -&gt; (a -&gt; a)</code>. In fact, all Haskell functions are curried (they only take one argument, and return a function to handle the rest), but thanks to operator associativity we can call them in whichever way makes sense for each situation.</p>]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Function strictness]]></title>
    <link href="http://davesquared.net/2012/03/function-strictness.html"/>
    <updated>2012-03-30T23:10:00+11:00</updated>
    <id>http://davesquared.net/2012/03/function-strictness</id>
    <content type="html"><![CDATA[<p>I&#8217;m currently reading <a href="http://www.amazon.com/Introduction-Functional-Programming-International-Computing/dp/0134841972/">Introduction to Functional Programming</a> by Richard Bird and Philip Wadler, and it contains a really nice explanation of function strictness, a topic that came up in my <a href="http://davesquared.net/2012/03/folds-pt3-left-fold-right.html">last post</a>.</p>

<p>Bird and Wadler describe a strict function as one that is undefined when its input is undefined. The <code>⊥</code> symbol, or <a href="http://en.wikipedia.org/wiki/Bottom_type">bottom</a>, is used to represent &#8216;undefined&#8217;, so <code>f</code> is strict if <code>f ⊥ = ⊥</code>, otherwise it is non-strict.</p>

<!-- more -->


<p>I interpret this to mean that if a function can take an undefined or generally awkward expression (like <code>undefined</code>, <code>1/0</code> or an infinite list) for one of its arguments and return a defined, sensible value then it is non-strict in that argument.</p>

<p>The first example they give for this is a function that returns a constant:</p>

<pre><code>three :: a -&gt; Int
three x = 3
{- Haskell GHCi session:
    *Main&gt; three 3
    3
    *Main&gt; three "hello"
    3
    *Main&gt; three undefined
    3
    *Main&gt; three (1/0)
    3
    *Main&gt; three [1..]
    3
-}
</code></pre>

<p>We can see that <code>three ⊥ = 3</code>, so it is non-strict. It never actually needs to evaluate its argument. So we can give it all kinds of wonderful arguments like<code>three (1/0)</code> (divide by zero), <code>three [1..]</code> (infinite list), <code>three (head [])</code> (the first element of an empty list) or even <code>three undefined</code>, and it will still happily return <code>3</code>.</p>

<p>Perhaps a more familiar example is <code>||</code>, the boolean <em>or</em> operator. It will only evaluate its second argument if the first is <code>False</code>:</p>

<pre><code>*Main&gt; True || False
True
*Main&gt; False || True
True
*Main&gt; False || False
False
*Main&gt; undefined || True
*** Exception: Prelude.undefined
*Main&gt; True || undefined
True
*Main&gt; False || undefined
*** Exception: Prelude.undefined
</code></pre>

<p>Here <code>⊥ || x = ⊥</code>, so <code>||</code> is strict in its first argument. But we also have <code>True || ⊥ = True</code>, so <code>||</code> is non-strict in its second argument when its first is <code>True</code>. This is how <code>||</code> is implemented in many languages (including C#, Ruby et al.), not just for functional languages like Haskell.</p>

<div class="note"><b>Aside:</b> I can only think of one language that strictly evaluates both arguments given to <i>or</i>, but I&#8217;m sure there are more. Leave a comment if you know of any. :)</div>


<h2>Non-strictness and laziness</h2>

<p>Let&#8217;s have a look at the related concept of laziness. Functional languages work by reducing expressions to their simplest forms (called <em>canonical</em> or <em>normal</em> form). There can be several approaches to doing this. Earlier we saw that Haskell could happily evaluate <code>three (1/0)</code>, even though dividing by zero isn&#8217;t generally a good idea. This was because Haskell chose to reduce this expression by first applying the definition of the function <code>three</code>; that is, <code>three</code> with any argument reduces to the value <code>3</code>.</p>

<p>A language could also decide to reduce the argument first. This would mean applying the rule of the division operation <code>(/)</code> first, then trying to apply the definition of <code>3</code>.</p>

<p>The first strategy is known as <em>lazy-evaluation</em>, while the second is the <em>eager-evaluation</em> us imperative folks are more familiar with. Imperative languages tend to evaluate arguments before calling a function, so trying to call <code>Three(SomeMethodThatThrows())</code> in C# will break where a non-strict, lazy language like Haskell doesn&#8217;t.</p>

<div class="note"><b>Aside:</b> We can work around this eager-evaluation in C# (and some other languages) with deferred execution using delegates / lambdas. For example, <code>Three(() => SomeMethodThatThrows())</code>.</div>


<p>As far as I can tell, <em>non-strictness</em> is the mathematical property we saw at the beginning of this post, while <em>lazy-evaluation</em> is the expression reduction strategy some languages like Haskell use to implement non-strictness.</p>

<h2>References</h2>

<ul>
<li>Richard Bird and Philip Wadler, <a href="http://www.amazon.com/Introduction-Functional-Programming-International-Computing/dp/0134841972/">Introduction to Functional Programming</a>.</li>
<li>Wikibooks, <a href="http://en.wikibooks.org/wiki/Haskell/Laziness">Haskell/Laziness</a>. Online, accessed 2012-03-30.</li>
<li>Haskell.org, <a href="http://www.haskell.org/haskellwiki/Lazy_vs._non-strict">Lazy vs. non-strict</a>. Online, accessed 2012-03-30.</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Folds Pt 3: Left fold, right?]]></title>
    <link href="http://davesquared.net/2012/03/folds-pt3-left-fold-right.html"/>
    <updated>2012-03-24T01:33:00+11:00</updated>
    <id>http://davesquared.net/2012/03/folds-pt3-left-fold-right</id>
    <content type="html"><![CDATA[<p>This post is part 3 of a <a href="http://davesquared.net/categories/-folds">series on folds</a>, which is my attempt to understand the topic. The examples are in Haskell, but hopefully you can follow along even if you&#8217;re not familiar with the language. If you find yourself getting lost in the syntax, I&#8217;ve written a <a href="http://davesquared.net/2012/02/haskell-newbie-attempts-a-haskell-quick-start.html">Haskell quick start</a> that goes through all the bits we&#8217;ll use.</p>

<p>In <a href="http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds.html">part 1</a> we found that a fold is a way of abstracting recursive operations over lists. In <a href="http://davesquared.net/2012/02/folds-pt2-from-loops-to-folds.html">part 2</a> we saw that we could also express loops using recursion, which we could extract into a different type of fold. We learned that the loop-like fold is called a <em>left fold</em> (it accumulates its values on the left of the expression), and that our original fold was called a <em>right fold</em> (which accumulates to the right).</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">foldLeft</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">a</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">a</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>
</span><span class='line'><span class="nf">foldLeft</span> <span class="n">f</span> <span class="n">acc</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">foldLeft</span> <span class="n">f</span> <span class="p">(</span><span class="n">f</span> <span class="n">acc</span> <span class="n">head</span><span class="p">)</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">foldLeft</span> <span class="kr">_</span> <span class="n">seed</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="n">seed</span>
</span><span class='line'>
</span><span class='line'><span class="nf">foldRight</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">b</span>
</span><span class='line'><span class="nf">foldRight</span> <span class="n">f</span> <span class="n">acc</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">f</span> <span class="n">head</span> <span class="p">(</span><span class="n">foldRight</span> <span class="n">f</span> <span class="n">acc</span> <span class="n">tail</span><span class="p">)</span>
</span><span class='line'><span class="nf">foldRight</span> <span class="kr">_</span> <span class="n">seed</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="n">seed</span>
</span></code></pre></td></tr></table></div></figure>


<p>We saw we could implement several functions using both left and right folds (like <code>sum</code>, <code>length</code>, and <code>mapFn</code>). As both folds abstract away explicit recursion, they seem capable of producing the same results, just with different orders of evaluation. It turns out these differences in evaluation have some important implications, so in this post we&#8217;ll take a closer look at them.</p>

<!-- more -->


<h2>Flipped arguments and evaluation order</h2>

<p>Both our folds take as arguments a function, a seed/accumulator, and a list. If we look closely at the type definitions of each, we can see that the first arguments differ:</p>

<pre><code>foldLeft :: (a -&gt; b -&gt; a) -&gt; a -&gt; [b] -&gt; a
foldRight :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b
</code></pre>

<p>The function passed to <code>foldLeft</code> is called with the accumulator, then an item from the list (<code>f acc head</code>), but <code>foldRight</code> is called with the list item then the accumulator (<code>f head acc</code>). The function arguments are flipped. This can make some folds easier to write in one form than the other. In particular, folds that create lists seem to more naturally fit with <code>foldRight</code>:</p>

<pre><code>addOneRight list = foldRight (\head acc -&gt; (head+1):acc) [] list
</code></pre>

<p>Here we add one to the list head, then prepend that onto the accumulated list using <code>(:)</code> (called <em>cons</em>, used for constructing lists).</p>

<p>If we try and write this using a left fold, the order of the list head and accumulated list arguments passed to our lambda function gets flipped:</p>

<pre><code>addOneLeft' list = foldLeft (\acc head -&gt; (head+1):acc) [] list
</code></pre>

<p>If we run these examples though, we get quite different results:</p>

<pre><code>*Main&gt; addOneRight [1..5]
[2,3,4,5,6]
*Main&gt; addOneLeft' [1..5]
[6,5,4,3,2]
</code></pre>

<p>This is due to the difference in evaluation order we identified between the fold types, although now it starts to have more impact than just where our parentheses end up. <code>foldRight</code> builds up an expression that evaluates from right to left, so our list gets built up like this:</p>

<pre><code>addOneRight [1..5] 
    = (1+1) : (addOneRight [2..5])
    = (1+1) : (2+1) : (addOneRight [3..5])
    = ...
    = (1+1) : (2+1) : (3+1) : (4+1) : (5+1) : []
    = [2,3,4,5,6]
</code></pre>

<p>Here our last list item <code>5</code> gets evaluated with our accumulator <code>[]</code> first. In other words, it is <a href="http://en.wikipedia.org/wiki/Operator_associativity">right-associative</a>; <code>foldRight (+) 0 [1..5]</code> will create the expression <code>1 + (2 + (3 + (4 + (5 + 0))))</code> which evaluates the right-most term first.</p>

<p>But <code>foldLeft</code> works more like a loop, and evaluates from left to right (it is left-associative; <code>foldLeft (+) 0 [1..5]</code> creates <code>((((0 + 1) + 2) + 3) + 4) + 5</code>), so our first list item <code>1</code> gets evaluated with the accumulator <code>[]</code> first:</p>

<pre><code>addOneLeft' [1..5]
    = addOneLeft' ((1+1) : []) [2..5]            -- Evals 1 with accumulator [] first.
    = addOneLeft' ((2+1) : (1+1) : []) [3..5]
    = ...
    = (5+1) : (4+1) : (3+1) : (2+1) : (1+1) : []
    = [6,5,4,3,2]
</code></pre>

<p>To fix our <code>addOneLeft</code> function, we want to append the item to the end of the accumulated list, rather than add it to the front. We can do this by using <code>(++)</code> which concatenates lists, and use <code>[head+1]</code> to convert the list element to a list so we can concatenate it.</p>

<pre><code>addOneLeft list = foldLeft (\acc head -&gt; acc ++ [head+1]) [] list
{-
addOneLeft [1..5]
    = addOneLeft ([] ++ [1+1]) [2..5]
    = addOneLeft ([] ++ [1+1] ++ [2+1]) [3..5]
    = ...
    = [] ++ [1+1] ++ [2+1] ++ [3+1] ++ [4+1] ++ [5+1]
    = [2,3,4,5,6]
-}
</code></pre>

<p>This gives us the <code>[2,3,4,5,6]</code> answer we expect.</p>

<p>So not only do the functions we pass to both folds differ in argument order, but the order the accumulated value is populated is also reversed.</p>

<h2>To infinity, and beyond!</h2>

<p>In the last section we saw that <code>foldLeft</code> and <code>foldRight</code> evaluate in opposite orders. This difference in evaluation order has another important impact, and that is how the different folds handle infinite lists.</p>

<h3>Taking just a bit of an infinite list</h3>

<p>First we&#8217;ll need to take a small digression to explain how Haskell works with infinite lists. The <code>take</code> function in Haskell grabs a specified number of elements from the start of a list. We can implement a basic version of this function so we can trace exactly what&#8217;s happening when we call it.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">take&#39;</span> <span class="ow">::</span> <span class="kt">Num</span> <span class="n">a</span> <span class="ow">=&gt;</span> <span class="n">a</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span>
</span><span class='line'><span class="nf">take&#39;</span> <span class="mi">0</span> <span class="kr">_</span> <span class="ow">=</span> <span class="kt">[]</span>              <span class="c1">-- The underscore &#39;_&#39; matches any argument value.</span>
</span><span class='line'><span class="nf">take&#39;</span> <span class="kr">_</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="kt">[]</span>
</span><span class='line'><span class="nf">take&#39;</span> <span class="n">i</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">head</span> <span class="kt">:</span> <span class="p">(</span><span class="n">take&#39;</span> <span class="p">(</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="n">tail</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can use our <code>take'</code> function like this:</p>

<pre><code>*Main&gt; take' 0 [1..10]
[]
*Main&gt; take' 5 [1..10]
[1,2,3,4,5]
*Main&gt; take' 3 [1..]
[1,2,3]
</code></pre>

<p>The first two calls take 0 and 5 elements respectively from a list of 10 numbers. The third call takes 3 elements from <code>[1..]</code>, which is Haskell for &#8220;an infinite list starting from 1&#8221;. We can get away with using infinite lists because Haskell uses <a href="http://en.wikipedia.org/wiki/Lazy_evaluation">lazy evaluation</a> (more precisely, it is <a href="http://www.haskell.org/haskellwiki/Lazy_vs._non-strict">non-strict</a>); it can evaluate only what it absolutely needs to in order to get a result from an expression.</p>

<p>If you type <code>[1..]</code> into GHCi, the interpreter will try and print this list to the terminal. Because printing every element in a list requires evaluating every element, this will continually spit out numbers until you stop it with <code>Ctrl + c</code> or something catastrophic happens to the process (like being stopped by the heat death of the universe).</p>

<p>If, instead of dumping <code>[1..]</code> to the terminal, we use it as an argument to our <code>take'</code> function, it will only evaluate the elements it needs from the list.</p>

<pre><code>take' 3 [1..]
    = 1 : take' 2 [2..]
    = 1 : 2 : take' 1 [3..]
    = 1 : 2 : 3 : take' 0 [4..]
    = 1 : 2 : 3 : []        -- We defined take' 0 of any list as []
</code></pre>

<p>Because <code>take' 0 [4..]</code> can return <code>[]</code> without further evaluation of the infinite list <code>[4..]</code>, Haskell returns the result without getting stuck in an infinite loop.</p>

<h3>Folding over infinite lists</h3>

<p>Let&#8217;s apply this knowledge to folds. I&#8217;ll try adding one to each element of an infinite list using our addOne functions, and only taking the first three elements from the result:</p>

<pre><code>*Main&gt; take' 3 (addOneRight [1..])
[2,3,4]
*Main&gt; take' 3 (addOneLeft [1..])
^CInterrupted.
</code></pre>

<p>Hmm. The <code>addOneRight</code> call worked fine, but <code>addOneLeft</code> just hung until I interrupted it using <code>Ctrl + c</code>. We&#8217;ve already seen how the <code>addOneRight</code> and <code>addOneLeft</code> functions are evaluated, so let&#8217;s trace through how that pattern works with infinite lists.</p>

<pre><code>addOneRight [1..]
    = (1+1) : addOneRight [2..]
    = (1+1) : (2+1) : addOneRight [3..]
    = (1+1) : (2+1) : (3+1) : ...

addOneLeft [1..]
    = addOneLeft ([] ++ [1+1]) [2..]
    = addOneLeft ([] ++ [1+1] ++ [2+1]) [3..]
    = addOneLeft ([] ++ [1+1] ++ [2+1] ++ [3+1] ++ ...) ...
</code></pre>

<p>The right fold version is able to evaluate elements straight away; first <code>(1+1)</code>, then <code>(2+1)</code> and so on, so <code>take'</code> can start consuming them immediately. The recursive call keeps generating elements off to the right, but if we stop needing to use the next element, Haskell&#8217;s going to be lazy and not evaluate the next recursive step.</p>

<pre><code>take' 3 (addOneRight [1..])
    = take' 3 ((1+1) : addOneRight [2..])
    = (1+1) : take' 2 (addOneRight [2..])
    = (1+1) : take' 2 ((2+1) : addOneRight [3..])
    = (1+1) : (2+1) : take' 1 (addOneRight [3..])
    = (1+1) : (2+1) : take' 1 ((3+1) : addOneRight [4..])
    = (1+1) : (2+1) : (3+1) : take' 0 (addOneRight [4..])
    = (1+1) : (2+1) : (3+1) : []        -- take' 0 of any list is [], we don't need to eval it.
</code></pre>

<p>In contrast, the left fold version can&#8217;t start evaluating until its recursive call reaches the end of the list.</p>

<pre><code>take' 3 (addOneLeft [1..])
    = take' 3 (addOneLeft ([] ++ [1+1]) [2..])
    = take' 3 (addOneLeft ([] ++ [1+1] ++ [2+1]) [3..])
    = take' 3 (addOneLeft ([] ++ [1+1] ++ [2+1] ++ [3+1]) [4..])
    = take' 3 (addOneLeft ([] ++ [1+1] ++ [2+1] ++ [3+1] ++ ...) ...)
</code></pre>

<p>So while <code>take'</code> was able to get a list of the form <code>(1+1) : rest</code> from <code>addOneRight</code> that it could start using straight away, the left fold has to completely evaluate <code>addOneLeft</code> before it gets access to a list in that form. And because that requires evaluating a function over every element in an infinite list, that&#8217;s going to take quite some time&#8230;</p>

<h3>Strictly thwarting our conquest of infinity</h3>

<p>So far we&#8217;ve seen that while left folds aren&#8217;t going to do us any favours when it comes to infinite lists, right folds seem to take them in their stride. There is a wrinkle here though; if the function we pass to our right fold needs to evaluate both its arguments (or its right argument first), then the recursion is not going to terminate and we&#8217;re going to end up hanging, just like our left fold.</p>

<p>The arguments a function needs to evaluate to produce a result is described as its <a href="http://www.haskell.org/haskellwiki/Non-strict_semantics">strictness</a>. A function that always needs to evaluate both its arguments is said to be strict in both its arguments. If it can sometimes get away with only evaluating some of its arguments it is non-strict (or, for example, just strict in its first argument).</p>

<p>An example of a function that is strict in both its arguments is <code>(+)</code>, which needs to evaluate both its left and right sides to return a result:</p>

<pre><code>*Main&gt; foldRight (+) 0 [1..10]
55
*Main&gt; foldRight (+) 0 [1..]
^CInterrupted.
</code></pre>

<p>While not being able to sum an infinite list does not come as a complete surprise, even code that we imagine could return a result early will not necessarily work. Let&#8217;s try and see if the sum of our list exceeds 5:</p>

<pre><code>*Main&gt; (&gt;5) ( foldRight (+) 0 [1..10] )
True
*Main&gt; (&gt;5) ( foldRight (+) 0 [1..] )
^CInterrupted.
</code></pre>

<p>Why doesn&#8217;t this stop as soon as the sum reaches 20?</p>

<pre><code>(&gt;20) (foldRight (+) 0 [1..])
    = (&gt;5) (1 + foldRight (+) 0 [2..])
    = (&gt;5) (1 + (2 + foldRight (+) 0 [3..]))
    = (&gt;5) (1 + (2 + (3 + foldRight (+) 0 [4..])))
    = ...
</code></pre>

<p>Even though our total is now greater than 5, our <code>(+)</code> operator isn&#8217;t able to give us a result because it needs to evaluate both its left and right sides. When it looks at <code>(1 + rest)</code>, it tries to evaluate <code>rest</code> to give us an answer, which brings it to <code>(1 + (2 + rest))</code>. Again it needs to evaluate <code>rest</code> to finish evaluating the original <code>(1 + rest)</code> call. So before we can check <code>(&gt;5)</code>, we have to wait until the infinite number of right-hand sides have been evaluated. And so again we reach for <code>Ctrl + c</code>.</p>

<p>This is different to our <code>take'</code> example which used the <code>(:)</code> and <code>(++)</code> operators, which are non-strict. If a function only needs the head of a list, it can grab that from <code>1:rest</code> without having to strictly evaluate the <code>rest</code> argument.</p>

<h2>Left or right?</h2>

<p>So how to we know when to choose a left fold over a right fold? I&#8217;m not entirely sure, but we can make some guesses based on what we&#8217;ve found so far.</p>

<p>First, if we want to work with infinite lists we&#8217;re going to need a right fold. It can be quite handy to have functions like <code>map</code> (<code>mapFn</code> in previous posts) work with infinite lists, as they can give us elegant ways of writing some functions (for example, finding all square numbers less than 1000: <code>takeWhile (&lt; 1000) (map (^2) [0..])</code>. Thanks to <a href="https://twitter.com/puffnfresh">@puffnfresh</a> for this example). So we&#8217;d write <code>map</code> as a right fold. This also means we would pick the right fold version of our <code>addOne</code> function (as it is a mapping of <code>(+1)</code> over a list).</p>

<p>If we can&#8217;t work with infinite lists (say, because we&#8217;re using a strict function like <code>(+)</code>), then we have a choice. We&#8217;ve seen arguments are passed in different orders to the function being folded, and the order of evaluation is reversed, either of which may better suit what we are trying to do with our fold. For example, reversing a list using <code>foldLeft (\acc h -&gt; h:acc) []</code>, or just <code>foldLeft (flip (:)) []</code>, seems quite neat.</p>

<p>I&#8217;ve seen suggestions to consider <a href="http://stackoverflow.com/a/1446803/906">loop-like vs. list recursion</a> and <a href="http://stackoverflow.com/a/1446478/906">operator associativity</a> in making a choice, but I&#8217;m not entirely sure how to apply these suggestions yet, so at the moment I&#8217;m stuck considering infinite lists and which order of evaluation suits what I&#8217;m trying to achieve.</p>

<p>When trying to work out how to write a fold and which variant to use, I find it useful to think about the accumulator (<code>acc</code>) argument in the function we&#8217;re folding as <em>delving</em> into the recursion. For left fold the function is <code>(\acc head -&gt; ...)</code>, while right is <code>(\head acc -&gt; ...)</code>, and the <code>acc</code> argument in both represents the result (or eventual result) of recursing over the rest of the list. This not only helps me to work out if a function can be folded right over an infinite list, but also helps me to work out how I need to implement or compose that function to give me the required result from my fold.</p>

<p>There is one other important difference between the folds we haven&#8217;t covered yet, and that is the question of efficiency in time taken and space used. We&#8217;ll look at this in the next post.</p>

<h2>Conclusion</h2>

<p>We&#8217;ve now seen a few key differences between left and right folds that give us an idea as to why we&#8217;ve bothered coming up with different ways of abstracting away recursion, despite both folds initially looking as if they produced identical results.</p>

<p>First we saw that <code>foldRight</code> takes a function with the accumulator passed in on the right, while <code>foldLeft</code> has it passed in on the left. Depending on the arguments required to the function being folded, this can make certain folds simpler to call in one direction than the other (such as being able to use <code>(:)</code> instead of <code>(++)</code> to build up lists while maintaining its order).</p>

<p>More importantly, we saw that the order of evaluation was different for the folds. Left folds start combining the first element with the initial accumulator, whereas for right folds the last element is the first combined with the initial accumulator value. This causes folding <code>(:)</code> using left fold to reverse a list, while the right fold preserves the initial order.</p>

<p>This evaluation order means left folds don&#8217;t work on infinite lists (it needs to get to the final element first before it can start evaluating the expression), while right folds (depending on a function&#8217;s strictness) can sometimes work happily with an infinite list. To determine whether a function will work with infinite lists, we need to think about whether that function can return a result without accessing its second parameter (<code>acc</code>, or accumulator).</p>

<p>We can use these differences to decide which type of fold to use. If we need to handle infinite lists then a right fold is our only choice. Otherwise we can consider which evaluation order seems to best suit what we&#8217;re trying to achieve. We are also yet to cover a final discriminator, the efficiency of different folds, and this will give us a bit more of a clue as to when we want to use each type of fold. We&#8217;ll look at this aspect in the next post of this series, starting with finding a significant problem with our current <code>foldLeft</code> implementation.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Folds Pt 2: From loops to folds]]></title>
    <link href="http://davesquared.net/2012/02/folds-pt2-from-loops-to-folds.html"/>
    <updated>2012-02-25T15:35:00+11:00</updated>
    <id>http://davesquared.net/2012/02/folds-pt2-from-loops-to-folds</id>
    <content type="html"><![CDATA[<p>This post is part 2 of a <a href="http://davesquared.net/categories/-folds">series on folds</a>. Folds seem to crop up quite often in functional programing, and this series is my attempt to learn a little about the topic.</p>

<p>In <a href="http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds.html">part 1</a> we found that a fold is a way of abstracting recursive operations over lists. In this post we&#8217;ll look at a different way of folding; one that traverses lists in a similar way to a <code>for</code>/<code>foreach</code> loop.</p>

<!-- more -->


<p>The examples are in Haskell, but I&#8217;m not going to assume you&#8217;ve had a chance to try it and so I&#8217;ll try to explain it as I go. Hopefully the concepts will make sense even if you&#8217;re not familiar with Haskell. If you find yourself getting lost in the syntax, I&#8217;ve written a <a href="http://davesquared.net/2012/02/haskell-newbie-attempts-a-haskell-quick-start.html">Haskell quick start</a> that goes through all the bits we&#8217;ll use.</p>

<h2>Previously, on davesquared.net&#8230;</h2>

<p><a href="http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds.html">Last time</a> we looked at recursive functions over lists like this one:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">add</span> <span class="ow">::</span> <span class="kt">Num</span> <span class="n">a</span> <span class="ow">=&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>    <span class="c1">-- Take a list of numbers, return a number</span>
</span><span class='line'><span class="nf">add</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">head</span> <span class="o">+</span> <span class="n">add</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">add</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="mi">0</span>
</span></code></pre></td></tr></table></div></figure>


<p>We defined our <code>add</code> function as returning the head of the list added to the result of recursively calling <code>add</code> on the rest of the list (known as the tail). We also defined a stopping condition for our recursion: when <code>add</code> is called with an empty list <code>[]</code>, it returns <code>0</code>. Tracing through a call to this function showed something like this:</p>

<pre><code>add [1,3,5]
    = add (1:[3,5])         -- split list into head and tail: 1 and [3,5]
    = 1 + (add [3,5])
    = 1 + (3 + (add [5]))
    = 1 + (3 + (5 + (add [])))
    = 1 + (3 + (5 + 0))     -- recursive calls now stopped. Time to evaluate the expression.
    = 1 + (3 + 5)
    = 1 + 8
    = 9
</code></pre>

<p>Here we evaluate the head of the list, and add it to the result of the expression accumulating on the right. Once the recursion reaches the stopping condition we&#8217;ve got a long line of brackets accumulated on the right, and we can start evaluating them all until they reduce back to our answer.</p>

<p>In the last post we looked at a few recursive functions like this, and found they all had a few things in common: a seed value to return for the empty list case, and a two argument function that is applied to the head of the list and the accumulated result of calling the original function on the tail of the list. Using this realisation we abstracted the common bits of these recursive operations into a <code>fold</code> function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">fold</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">b</span>
</span><span class='line'><span class="nf">fold</span> <span class="n">f</span> <span class="n">seed</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">f</span> <span class="n">head</span> <span class="p">(</span><span class="n">fold</span> <span class="n">f</span> <span class="n">seed</span> <span class="n">tail</span><span class="p">)</span>
</span><span class='line'><span class="nf">fold</span> <span class="kr">_</span> <span class="n">seed</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="n">seed</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- Re-writing add function using fold:</span>
</span><span class='line'><span class="nf">addWithFold</span> <span class="ow">::</span> <span class="kt">Num</span> <span class="n">a</span> <span class="ow">=&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>
</span><span class='line'><span class="nf">addWithFold</span> <span class="n">list</span> <span class="ow">=</span> <span class="n">fold</span> <span class="p">(</span><span class="o">+</span><span class="p">)</span> <span class="mi">0</span> <span class="n">list</span>
</span><span class='line'>    <span class="c1">-- (+) is a two arg function. In lambda syntax: (\x y -&gt; x + y)</span>
</span></code></pre></td></tr></table></div></figure>


<p>And this <code>addWithFold</code> function worked exactly the same as our original <code>add</code> function, except we no longer have to explicitly use recursion, it&#8217;s abstracted away in our <code>fold</code> function. Tracing through a call to <code>addWithFold</code> showed it expands out exactly as per our original <code>add [1,3,5]</code> example above, including the expressions accumulating on the right (you can see an example of tracing through a fold in <a href="http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds.html">part 1</a>).</p>

<p>And that&#8217;s where we left things last post.</p>

<h2>Writing <code>add</code> using a loop</h2>

<p>What if we rewrote our original, recursive version of <code>add</code> using a standard, imperative loop? Let&#8217;s see how that would look in a C-like language (in this case, C#):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="kt">int</span> <span class="nf">Add</span><span class="p">(</span><span class="kt">int</span><span class="p">[]</span> <span class="n">numbers</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">int</span> <span class="n">acc</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span>
</span><span class='line'>    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">=</span><span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="n">numbers</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">acc</span> <span class="p">+=</span> <span class="n">numbers</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">acc</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>We initialise an accumulator <code>acc</code> to 0, then keep adding each member of the list of numbers to it before returning the final value of <code>acc</code>.</p>

<p>Let&#8217;s use a little bit of imagination now. Say C# was actually a language with <a href="http://en.wikipedia.org/wiki/Lazy_evaluation">lazy evaluation</a> like Haskell, so it does not actually evaluate any functions like <code>(+)</code> until it absolutely has to. If we were to trace through the execution of the loop in this lazy world we&#8217;d see something like this:</p>

<pre><code>Add(new[] { 1, 3, 5 });
    acc = 0                     // Initial value
    acc = (0 + 1)               // 1st iteration
    acc = ((0 + 1) + 3)         // 2nd iteration
    acc = (((0 + 1) + 3) + 5)   // 3rd iteration and end of loop
    acc = ((1 + 3) + 5)         // Start evaluating...
    acc = (4 + 5)
    acc = 9                     // ... return result
</code></pre>

<p>Notice that the here our expression is building up on the left side, instead of on the right-hand side we saw for our version which used <code>fold</code>?</p>

<pre><code>    -- Loop version:
    ((0 + 1) + 3) + 5

    -- Fold version:
    1 + (3 + (5 + 0))
</code></pre>

<p>Well, we could also write a Haskell function that uses an accumulator too. But Haskell doesn&#8217;t have imperative-style loops, so we&#8217;ll need to go back to recursion.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">add&#39;</span> <span class="n">list</span> <span class="ow">=</span>
</span><span class='line'>    <span class="kr">let</span> <span class="n">accumulate</span> <span class="n">acc</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">accumulate</span> <span class="p">(</span><span class="n">acc</span><span class="o">+</span><span class="n">head</span><span class="p">)</span> <span class="n">tail</span>
</span><span class='line'>        <span class="n">accumulate</span> <span class="n">acc</span> <span class="kt">[]</span>          <span class="ow">=</span> <span class="n">acc</span>
</span><span class='line'>    <span class="kr">in</span> <span class="n">accumulate</span> <span class="mi">0</span> <span class="n">list</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here we&#8217;ve used <code>let</code> to introduce a kind of nested function <code>accumulate</code> within <code>add'</code>. It includes an extra variable <code>acc</code> to store our accumulated value. The <code>accumulate</code> function recursively calls itself until it stops at the empty list. Our <code>add'</code> function kicks off this recursion with the initial accumulator value by calling <code>accumulate 0 list</code>.</p>

<p>Now look at what happens when we trace through a call to <code>add'</code>:</p>

<pre><code>add' [1,3,5]
    = accumulate 0 [1,3,5]
    = accumulate (0 + 1) [3,5]
    = accumulate ((0 + 1) + 3) [5]
    = accumulate (((0 + 1) + 3) + 5) []  -- stopping condition for recursion, returns acc
    = (((0 + 1) + 3) + 5)                -- recursive calls now stopped. Time to evaluate
    = ((1 + 3) + 5)
    = (4 + 5)
    = 9
</code></pre>

<p>Look familiar? This results in the same <code>((0 + 1) + 3) + 5</code> expression that is produced by the looping version of the function. We&#8217;re accumulating our values in parentheses on the left hand side of our expression.</p>

<h2>More examples of loop-style recursions</h2>

<p>In <a href="http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds.html">part 1</a> we implemented a few different functions using recursion that built up expressions on their right hand sides. We can also implement these using loop-like recursion as we did for <code>add'</code>. Here are <code>len</code> and <code>mapFn</code> from the previous post, rewritten to accumulate values on the left hand side:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">len</span> <span class="ow">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">len</span> <span class="n">list</span> <span class="ow">=</span>
</span><span class='line'>    <span class="kr">let</span> <span class="n">accumulate</span> <span class="n">acc</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">accumulate</span> <span class="p">(</span><span class="n">acc</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="n">tail</span>
</span><span class='line'>        <span class="n">accumulate</span> <span class="n">acc</span> <span class="kt">[]</span>          <span class="ow">=</span> <span class="n">acc</span>
</span><span class='line'>    <span class="kr">in</span> <span class="n">accumulate</span> <span class="mi">0</span> <span class="n">list</span>
</span><span class='line'>
</span><span class='line'><span class="nf">mapFn</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span>
</span><span class='line'><span class="nf">mapFn</span> <span class="n">f</span> <span class="n">list</span> <span class="ow">=</span>
</span><span class='line'>    <span class="kr">let</span> <span class="n">accumulate</span> <span class="n">acc</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">accumulate</span> <span class="p">(</span><span class="n">acc</span> <span class="o">++</span> <span class="p">[</span><span class="n">f</span> <span class="n">head</span><span class="p">])</span> <span class="n">tail</span>  <span class="c1">-- (++) concats lists</span>
</span><span class='line'>        <span class="n">accumulate</span> <span class="n">acc</span> <span class="kt">[]</span>          <span class="ow">=</span> <span class="n">acc</span>
</span><span class='line'>    <span class="kr">in</span> <span class="n">accumulate</span> <span class="kt">[]</span> <span class="n">list</span>
</span><span class='line'>
</span><span class='line'><span class="cm">{-</span>
</span><span class='line'><span class="cm">*Main&gt; len [1..10]</span>
</span><span class='line'><span class="cm">10</span>
</span><span class='line'><span class="cm">*Main&gt; mapFn (*2) [1,3,5,7]</span>
</span><span class='line'><span class="cm">[2,6,10,14]</span>
</span><span class='line'><span class="cm">-}</span>
</span></code></pre></td></tr></table></div></figure>


<p>All these functions look strikingly similar. They perform an operation with the <code>acc</code> accumulator and the <code>head</code> of the list, and make a recursive call with this value as the new accumulator. And they all start off with an initial case, commonly called a <em>seed</em>. For <code>add'</code> and <code>len</code> the seed is 0, for <code>mapFn</code> it&#8217;s <code>[]</code>.</p>

<p>Last post we were able to generalise the common parts of recursive functions into a <code>fold</code> function that accumulated values on the right hand side. Can we write a fold function that evaluates in this loop-like way instead?</p>

<h2>Looping using a fold</h2>

<p>They key areas of difference between all these functions is the initial seed used for the accumulator, and the operation we perform on the accumulator <code>acc</code> and the head of the list. This means that, similar to the last time we wrote fold, we&#8217;re going to have to pass in a seed and a function to operate on <code>acc</code> and head. And we&#8217;ll need to pass in a list to fold over.</p>

<h3>Type detective work</h3>

<p>Let&#8217;s try and work out what the types of these arguments need to be. Here&#8217;s the types we need to figure out, with placeholders (indicated by question marks) for the argument types and the return type:</p>

<pre><code>foldLeft :: (function?) -&gt; seed? -&gt; list? -&gt; return?
</code></pre>

<p>First let&#8217;s take a look at the seed. For <code>add'</code> and <code>len</code> we passed in <code>0</code>, an <code>Int</code>, and this is the type we ended up returning from the functions. For <code>mapFn</code> we passed in a list <code>[]</code>, and we ended up returning a list which was the result of applying the mapping to each element. So it looks like the seed and the return value have to be the same type, but it doesn&#8217;t really matter what that type is. Let&#8217;s just call it type <code>a</code> for now.</p>

<pre><code>foldLeft :: (function?) -&gt; a -&gt; list? -&gt; a
</code></pre>

<p>Next let&#8217;s look at the list we pass in. For <code>add'</code> this was <code>[Int]</code>, a list of <code>Int</code>. For <code>len</code> and <code>mapFn</code> it was any type of list. So this can really be any type of list, and it doesn&#8217;t have to be the same type that is used for the seed or the function return value (for example <code>len</code> takes any list <code>[a]</code> and returns an <code>Int</code> which represents the length of the list). So we&#8217;ll say the list elements are of any type <code>b</code>, which means it can be any type, but that it does not have to be the same type as our accumulator of type <code>a</code>.</p>

<pre><code>foldLeft :: (function?) -&gt; a -&gt; [b] -&gt; a
</code></pre>

<p>The last type we need to figure out is the function that operates on the accumulator <code>acc</code> and the head of the list and returns the new value of the accumulator. For <code>add'</code>, this function was <code>acc + head</code>. We already decided that our accumulator is of type <code>a</code>. We also know our list elements are of type <code>b</code>, so the head of the list that gets passed into the function will be something of type <code>b</code>. And then our function returns the new value of the accumulator, which will need to be the same type <code>a</code> as our previous accumulator. So the function will need to have the type <code>a -&gt; b -&gt; a</code>.</p>

<p>All this gives the following type definition:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">foldLeft</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">a</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">a</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here we have 3 arguments: a function that takes an accumulator of type <code>a</code> and a list head of type <code>b</code> and returns the new accumulator; a value of type <code>a</code> as the seed for the accumulator; and a list <code>[b]</code>. Then our function will return the final accumulated value of type <code>a</code>.</p>

<p>I find reasoning about all the types that go flying around quite tricky, but I&#8217;ve also found understanding them is a major part of the battle.</p>

<h3>Implementing the left fold</h3>

<p>Now we need to try and fit all of this together. I&#8217;m going to use our <code>add'</code>, <code>len</code> and <code>mapFn</code> examples as a template:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">foldLeft</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">a</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">a</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>
</span><span class='line'><span class="nf">foldLeft</span> <span class="n">f</span> <span class="n">seed</span> <span class="n">list</span> <span class="ow">=</span>
</span><span class='line'>    <span class="kr">let</span> <span class="n">accumulate</span> <span class="n">acc</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">accumulate</span> <span class="p">(</span><span class="n">f</span> <span class="n">acc</span> <span class="n">head</span><span class="p">)</span> <span class="n">tail</span>
</span><span class='line'>        <span class="n">accumulate</span> <span class="n">acc</span> <span class="kt">[]</span>          <span class="ow">=</span> <span class="n">acc</span>
</span><span class='line'>    <span class="kr">in</span> <span class="n">accumulate</span> <span class="n">seed</span> <span class="n">list</span>
</span></code></pre></td></tr></table></div></figure>


<p>This follows the same format as our previous functions. The only difference is that we&#8217;re passing in <code>f</code> and calling <code>f acc head</code> to get the new accumulator value, and that we&#8217;re passing in a <code>seed</code>. We can simplify this a bit because we already have the seed argument to <code>foldLeft</code> that we can use as the accumulator (so we don&#8217;t need to declare an inner <code>accumulate</code> function):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">foldLeft</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">a</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">a</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>
</span><span class='line'><span class="nf">foldLeft</span> <span class="n">f</span> <span class="n">acc</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">foldLeft</span> <span class="n">f</span> <span class="p">(</span><span class="n">f</span> <span class="n">acc</span> <span class="n">head</span><span class="p">)</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">foldLeft</span> <span class="kr">_</span> <span class="n">seed</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="n">seed</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we trace through an example we&#8217;ll see it works exactly the same as our previous, loop-like recursion:</p>

<pre><code>foldLeft (+) 0 [1,3,5]
    = foldLeft (+) (0 + 1) [3,5]
    = foldLeft (+) ((0 + 1) + 3) [5]
    = foldLeft (+) (((0 + 1) + 3) + 5) []
    = (((0 + 1) + 3) + 5)
    = ...
    = 9
</code></pre>

<p>Now we are able to rewrite our other functions without having to use explicit recursion:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">lenLeft</span> <span class="ow">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">lenLeft</span> <span class="n">list</span> <span class="ow">=</span> <span class="n">foldLeft</span> <span class="p">(</span><span class="nf">\</span><span class="n">acc</span> <span class="n">head</span> <span class="ow">-&gt;</span> <span class="n">acc</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="mi">0</span> <span class="n">list</span>
</span><span class='line'>
</span><span class='line'><span class="nf">mapFnLeft</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span>
</span><span class='line'><span class="nf">mapFnLeft</span> <span class="n">f</span> <span class="n">list</span> <span class="ow">=</span> <span class="n">foldLeft</span> <span class="p">(</span><span class="nf">\</span><span class="n">acc</span> <span class="n">head</span> <span class="ow">-&gt;</span> <span class="n">acc</span> <span class="o">++</span> <span class="p">[</span><span class="n">f</span> <span class="n">head</span><span class="p">])</span> <span class="kt">[]</span> <span class="n">list</span>
</span><span class='line'>
</span><span class='line'><span class="cm">{-</span>
</span><span class='line'><span class="cm">*Main&gt; lenLeft [1..10]</span>
</span><span class='line'><span class="cm">10</span>
</span><span class='line'><span class="cm">*Main&gt; mapFnLeft (*2) [1..10]</span>
</span><span class='line'><span class="cm">[2,4,6,8,10,12,14,16,18,20]</span>
</span><span class='line'><span class="cm">-}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Left or right?</h2>

<p>We now have two versions of folding: <code>foldLeft</code> that works like a loop and accumulates values on the left hand side, and <code>foldRight</code> that accumulates values on the right.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">foldLeft</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">a</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">a</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>
</span><span class='line'><span class="nf">foldLeft</span> <span class="n">f</span> <span class="n">acc</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">foldLeft</span> <span class="n">f</span> <span class="p">(</span><span class="n">f</span> <span class="n">acc</span> <span class="n">head</span><span class="p">)</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">foldLeft</span> <span class="kr">_</span> <span class="n">seed</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="n">seed</span>
</span><span class='line'>
</span><span class='line'><span class="nf">foldRight</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">b</span>
</span><span class='line'><span class="nf">foldRight</span> <span class="n">f</span> <span class="n">seed</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">f</span> <span class="n">head</span> <span class="p">(</span><span class="n">foldRight</span> <span class="n">f</span> <span class="n">seed</span> <span class="n">tail</span><span class="p">)</span>
</span><span class='line'><span class="nf">foldRight</span> <span class="kr">_</span> <span class="n">seed</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="n">seed</span>
</span></code></pre></td></tr></table></div></figure>


<p>These functions are provided in the standard Haskell Prelude library as <code>foldl</code> and <code>foldr</code>. Both are used to abstract away explicit recursion, and other than our observation about the order it evaluates expression, they both give the same results.</p>

<p>It turns out that this order of evaluation can make a world of difference, especially in a lazily-evaluted language like Haskell. In the <a href="http://davesquared.net/2012/03/folds-pt3-left-fold-right.html">next post</a> in this series, we&#8217;ll look at some of these differences.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Goals for software developers]]></title>
    <link href="http://davesquared.net/2012/02/goals-for-devs.html"/>
    <updated>2012-02-19T23:07:00+11:00</updated>
    <id>http://davesquared.net/2012/02/goals-for-devs</id>
    <content type="html"><![CDATA[<p>Most software developers I know are really keen to improve their skills. But how do we know we&#8217;re improving? How can we tell if our efforts are effective? Without feedback on our progress, it is easy to feel swamped and demoralised by the rate at which stuff we want to learn piles up, compared with the rate at which we feel we&#8217;re improving. Setting and working towards goals can help address this.</p>

<p>Yet when I&#8217;ve asked other devs, most have great personal, work and non-tech goals, but have something to the effect of &#8220;become a better developer&#8221; thrown in as well. I&#8217;ve been thinking about this a bit recently, so thought I&#8217;d jot down a couple of thoughts about goals specifically related to software development.</p>

<!-- more -->


<h2>Goals are important</h2>

<p>Goals have the potential to motivate us, help us focus, get into a <a href="http://en.wikipedia.org/wiki/Flow_(psychology">flow</a>, avoid wasting energy, make the most of opportunities we have and, when we reach them, give us a feeling of achievement and growth, along with the happiness that brings.</p>

<p>Many (if not all) of the people I know that I consider really successful are very goal-oriented. They know what they want to achieve and have the skill, self-confidence, and drive to make it happen. For them it seems to come naturally, but I think it is an approach that can be learned and adopted by anyone, irrespective of personality. It&#8217;s simply a matter of choosing the right goals; ones that inspire and provide both feedback on progress and a feeling of achievement on meeting them.</p>

<h2>Goals with external dependencies</h2>

<p>One thing I&#8217;ve found really useful in helping to pick goals is to consider which ones depend solely on me, and which ones depend on other people.</p>

<p>Goals like getting a certain number of blog readers, presentations at user groups and conferences, or community recognition all rely on other people. Sure, you have some ability to influence these measures, but the final result is out of your hands. You are not the one in control of whether you meet your goal.</p>

<p>Now I&#8217;ve seen these kind of goals work great for people as a way to become better developers (higher profile leads to more opportunities and exchanges of ideas with other top devs), but there are a few reasons they don&#8217;t work for me. For one these goals just don&#8217;t inspire me, perhaps because I&#8217;m a bit of an introvert. Also I don&#8217;t have much success in these areas, so you could attribute this to sour grapes ;). But most importantly, I find making progress towards goals plays a crucial part in my motivation and happiness, and I&#8217;m not willing to chance either of those things by making them dependent on other people who aren&#8217;t even aware of the burden I&#8217;ve placed on them.</p>

<p>So for me I need goals that are largely dependent on my own actions.</p>

<h2>Non-specific goals</h2>

<p>I love learning. I love the process of piecing together little bits of information, until finally something clicks, accompanied by the joy of discovering something I didn&#8217;t know before. I guess it&#8217;s no surprise then that a number of my goals have been things like &#8220;learn (TDD|WPF|Haskell)&#8221;.</p>

<p>These make horrible (albeit well-intentioned) goals. The problem is determining when I have achieved them. When have I &#8220;learned functional programming&#8221;? One of the main things I learn on any topic is becoming aware of more stuff I don&#8217;t know &#8211; the unknown unknowns. This makes it very tough to get the motivating feeling of progress or the joy of reaching a goal.</p>

<p>These make great, general directions for us to follow, but as goals they just don&#8217;t cut it.</p>

<h2>Concrete goals for software developers</h2>

<p>We&#8217;ve ruled out a few types of goals for me so far: goals with external dependencies, and goals based solely on my areas of interest (like learning tech X). Where does that leave me if I want to improve as a software developer? It pretty much just leaves building stuff.</p>

<p>Rather than trying to learn technology X, build a project with technology X. The area of interest sets our direction, and we set an achievable, unambiguous goal out in that direction. This gives us something concrete to focus on, a measure of progress against the project requirements, and the ability to complete our goal and say we&#8217;re done. And we&#8217;ve got the added benefit that we don&#8217;t need 10 people to click our subscribe links, or 3 conferences to accept our talk proposals. It&#8217;s completely up to us to work towards our goal, with no external pressures to stifle our creativity, taking us in a direction we&#8217;re keen to go, inspired by frequent feedback on our progress, and with the joys of both learning and creating something new.</p>

<p>In keeping with <a href="http://en.wikipedia.org/wiki/SMART_criteria">standard guidance for goals</a>, good projects to choose are probably simple, small projects that don&#8217;t have to (but can) culminate in polished, shippable products, but are enough to learn and apply the skills being sought. The important things are to make sure it is something that you can build that unambiguously qualifies as achieving the stated goal, and that you&#8217;re enthusiastic about it.</p>

<p>A related approach that I&#8217;ve found useful is &#8220;building&#8221; a blog post on the topic I&#8217;m interested in. While it&#8217;s a bit more ambiguous as to the success I&#8217;ve had in meeting my goal, it does provide a way to develop my understanding and relate it to concrete output.</p>

<h2>Generating ideas for projects</h2>

<p>I thought it was worthwhile to quickly mention this. It can be tough to come up with usable project ideas, so whenever I do come up with one I note it somewhere safe (in a &#8220;trusted system&#8221; as <a href="http://www.davidco.com/about-gtd">David Allen</a> would call it). Paper, notebook, task list app, email it to yourself, whatever. Just make sure it is captured and accessible. I&#8217;ve found the more I do this, the more ideas I seem to get (although admittedly I still struggle to get lots of inspiring ideas. I do feel capturing everything is slowly helping me improve though). If you&#8217;re really stuck try looking up code katas or exercises like <a href="http://projecteuler.net/">Project Euler</a>. But the best projects will be ones that serve a need you have, or fit in with a hobby or subject that really interests you.</p>

<h2>Conclusion</h2>

<p>Many developers I&#8217;ve spoken with have the aim of becoming better developers, but haven&#8217;t taken the step of defining what that means. I&#8217;ve found it helpful to start translating this into specific goals, and the ones that seem to work best for me are building small projects that relate to an area of interest. I&#8217;m also careful to avoid goals that don&#8217;t work for me, such as those that depend on external factors and non-specific goals.</p>

<p>If you&#8217;ve found other software dev-specific goals and related approaches that work for you, I&#8217;d love to hear about them!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Haskell newbie attempts a Haskell quick start guide]]></title>
    <link href="http://davesquared.net/2012/02/haskell-newbie-attempts-a-haskell-quick-start.html"/>
    <updated>2012-02-09T12:02:00+11:00</updated>
    <id>http://davesquared.net/2012/02/haskell-newbie-attempts-a-haskell-quick-start</id>
    <content type="html"><![CDATA[<p>I recently posted an <a href="http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds.html">attempt to explain folds using Haskell</a>, and I got some feedback that the code samples were quite hard to follow for people that hadn&#8217;t played with Haskell before. Now the best way that I know of to quickly get started with Haskell is to go through the excellent <a href="http://learnyouahaskell.com/">Learn You a Haskell for Great Good</a> guide. I heartily recommend leaving this post right now and reading it. The whole guide is available free online, but you can also buy print copies of the fantastic reference. Here&#8217;s <a href="http://learnyouahaskell.com/">the link</a> again in case you missed it the first time.</p>

<p>Are you still here? Oh. Well, seeing as you don&#8217;t seem to want to listen to my advice, you deserve the rest of this post. It features a complete Haskell-n00b sharing his ignorance and misinformation with a reckless disregard for your quest to understand the basics of Haskell in a way that may permanently impair your ability to learn Haskell, understand functional programming, operate heavy machinery, socialise with other humans or be trusted with the weighty responsibility of goldfish ownership.</p>

<p>The aim of this post, despite your reticence to go through an <a href="http://learnyouahaskell.com/">extraordinarily helpful Haskell tutorial</a>, is to get going with the very basics of Haskell insanely quickly and with a minimum of understanding about what&#8217;s actually going on. Because that&#8217;s just the way I roll. And let&#8217;s face it, you deserve it for not taking my advice. ;) (<a href="http://learnyouahaskell.com/">Last chance</a> for redemption.)</p>

<!-- more -->


<h2>Getting to a Haskell interactive prompt</h2>

<p>Grab the Haskell Platform from <a href="http://www.haskell.org/">Haskell.org</a>. Once installed, go to your favourite terminal and type <code>ghci</code> to load the <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/index.html">Glorious Glasgow Haskell Compilation System</a> interactive environment (assuming <code>ghci</code> is on your path). You should see GHCi&#8217;s <code>Prelude&gt;</code> prompt. If you type in something profound like <code>7*6</code> and hit return you should get an answer of sorts.</p>

<p>Don&#8217;t do it now, but when you want to exit, type <code>:q</code> (in glorious vim tradition).</p>

<h2>Loading a Haskell file</h2>

<p>Create a blank file called <code>haskell-misinformation.hs</code> in the same directory you ran <code>ghci</code> from. Actually, you can call it whatever you want, but it should end in <code>.hs</code>. Now jump back to your GHCi prompt and type <code>:l haskell-misinformation.hs</code> (you get tab for autocomplete). This will load your file into GHCi. Now whenever we make changes to our Haskell file, we just need to type <code>:r</code> to reload it and pick up our changes.</p>

<p>Here&#8217;s what it looks like on my machine:</p>

<pre><code>~  % ghci
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&gt; :l haskell-misinformation.hs 
[1 of 1] Compiling Main             ( haskell-misinformation.hs, interpreted )
Ok, modules loaded: Main.
*Main&gt; :r
Ok, modules loaded: Main.
*Main&gt; 
</code></pre>

<h2>Writing a function</h2>

<p>Let&#8217;s write an function that takes an integer and increments it. In our Haskell file, type:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">increment</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">increment</span> <span class="n">i</span> <span class="ow">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>Switch back to GHCi and reload our Haskell file by typing <code>:r</code> and hitting return (from now on, we&#8217;ll abbreviate this to &#8220;reload the file&#8221;). If we then type <code>increment 3</code> into GHCi we should get the unsurprising result of <code>4</code>.</p>

<pre><code>*Main&gt; :r
[1 of 1] Compiling Main             ( haskell-misinformation.hs, interpreted )
Ok, modules loaded: Main.
*Main&gt; increment 3
4
</code></pre>

<p>Notice we don&#8217;t need any parentheses for our function call? We just put the arguments straight after the function name.</p>

<p>Let&#8217;s take a close look at this syntax. The first line is the type declaration of our <code>increment</code> function. The <code>Int -&gt; Int</code> bit shows the arguments and return type to our function. In this case, <code>increment</code> takes an <code>Int</code> and returns an <code>Int</code>. At first this confused me, having argument types and return types all bundled together like that, with <code>-&gt;</code> arrows thrown around somewhat haphazardly, but it ends up making a lot of sense later on.</p>

<p>But we&#8217;re not here to make a lot of sense. We&#8217;re here to get started with Haskell quickly. Let&#8217;s move on before any accidental learning takes place!</p>

<h2>Writing another function</h2>

<p>Let&#8217;s write an <code>add</code> function that adds two numbers together. Before we do, let&#8217;s try and work out that weird, arrow-filled type signature. Our function is going to take 2 <code>Int</code> arguments, and return an <code>Int</code>. So let&#8217;s write all that out separated by arrows:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">add</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">add</span> <span class="n">a</span> <span class="n">b</span> <span class="ow">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</span></code></pre></td></tr></table></div></figure>


<p>Reload the file and run <code>add 3 39</code>. As we saw ealier we don&#8217;t use parentheses when calling functions, and now we can see that multiple arguments are separated by spaces. GHCi helpfully tells us the answer is <code>42</code>. Amazing! Is there anything this Haskell thing can&#8217;t do?</p>

<h2>Fun with function arguments</h2>

<p>Before we go on, try typing <code>:t add</code> into GHCi. It should return <code>add :: Int -&gt; Int -&gt; Int</code>, the type declaration of our function. You can use that to query the types of lots of things, so it&#8217;s a handy trick to remember.</p>

<p>Now try typing <code>:t add 5</code>. I get this:</p>

<pre><code>*Main&gt; :t add 5
add 5 :: Int -&gt; Int
</code></pre>

<p>Hmmm. The type of <code>add</code> is <code>Int -&gt; Int -&gt; Int</code>, which means it takes 2 integers and returns an integer. The type of <code>add 5</code> is <code>Int -&gt; Int</code>, which means it takes 1 integer and returns 1 integer. We gave <code>add</code> its first argument and the result is another function which takes the rest of the arguments.</p>

<p>Let&#8217;s put that into a new function in our Haskell file and reload it. Our file should look something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">increment</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">increment</span> <span class="n">i</span> <span class="ow">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="nf">add</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">add</span> <span class="n">a</span> <span class="n">b</span> <span class="ow">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</span><span class='line'>
</span><span class='line'><span class="nf">addFive</span> <span class="ow">=</span> <span class="n">add</span> <span class="mi">5</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice we didn&#8217;t put the type declaration in for <code>addFive</code>? That&#8217;s because Haskell can infer that for us. In fact, we didn&#8217;t need to put in any of the previous ones either. Haskell can work those out too. Hah! Made you type for nothing! Actually I did have my reasons; I feel getting to grips with the type declarations is really important for using Haskell effectively.</p>

<p>Now if we call <code>addFive 10</code> in GHCi we get <code>15</code>. This may strike you as a bit strange. Where is <code>addFive</code>&#8217;s argument? Shouldn&#8217;t we have written <code>addFive a = add 5 a</code>?</p>

<p>Well, that would work too, but we already used <code>:t</code> to determine <code>add 5</code> is a function that takes one integer and returns another. By writing <code>addFive = add 5</code> we have just given that function a name.</p>

<p>This is called partial function application. If we just give a few of the arguments a function requires, it will return a function that takes the remainder of the arguments. As an aside, this is the reason for the funny <code>-&gt;</code> symbols in the function type declaration; <a href="http://www.haskell.org/haskellwiki/Currying">all Haskell functions take just one argument</a> (<em>warning:</em> don&#8217;t click that link if you are feeling lost. You don&#8217;t need partial application to understand the rest of this post).</p>

<h2>When we don&#8217;t know the exact type</h2>

<p>So far our type declarations have all involved <code>Int</code>. What if we want to return <code>42</code> for any argument of any type? After all, it is the meaning of life, the universe and everything. For that we can use a lowercase placeholder to represent that type (Haskell convention is to use a single character, but I think any string that begins with a lowercase char will do). In this case we&#8217;ll use <code>a</code>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">meaningOfLife</span> <span class="ow">::</span> <span class="n">a</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">meaningOfLife</span> <span class="n">x</span> <span class="ow">=</span> <span class="mi">42</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the function body we&#8217;re putting the argument we get into a variable named <code>x</code>, but no matter what that variable contains, the answer is always 42.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="o">*</span><span class="kt">Main</span><span class="o">&gt;</span> <span class="n">meaningOfLife</span> <span class="mi">123</span>
</span><span class='line'><span class="mi">42</span>
</span><span class='line'><span class="o">*</span><span class="kt">Main</span><span class="o">&gt;</span> <span class="n">meaningOfLife</span> <span class="mi">20</span>
</span><span class='line'><span class="mi">42</span>
</span><span class='line'><span class="o">*</span><span class="kt">Main</span><span class="o">&gt;</span> <span class="n">meaningOfLife</span> <span class="mf">3.1416</span>
</span><span class='line'><span class="mi">42</span>
</span><span class='line'><span class="o">*</span><span class="kt">Main</span><span class="o">&gt;</span> <span class="n">meaningOfLife</span> <span class="s">&quot;hello world&quot;</span>
</span><span class='line'><span class="mi">42</span>
</span><span class='line'><span class="o">*</span><span class="kt">Main</span><span class="o">&gt;</span> <span class="n">meaningOfLife</span> <span class="kt">[]</span>
</span><span class='line'><span class="mi">42</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Lists</h2>

<p>Haskell has a list type that we can construct using square brackets, as I&#8217;ve done in the following GHCi session:</p>

<pre><code>*Main&gt; [1,2,3,4,5,6,7,8]
[1,2,3,4,5,6,7,8]
*Main&gt; [1..8]
[1,2,3,4,5,6,7,8]
*Main&gt; [1,3..8]
[1,3,5,7]
*Main&gt; []
[]
</code></pre>

<p>Lists can also be constructed using the <em>cons</em> operator, which in Haskell is <code>:</code>. The <code>:</code> operator takes two arguments. The first is the element you want to add to the front of a list, and the second is an existing list (bonus points if you can work out what the type declaration for this will be before running <code>:t (:)</code>). So we can add 1 to the front of an empty list, or to an existing list:</p>

<pre><code>*Main&gt; 1:[]
[1]
*Main&gt; 1:[2,3,4,5]
[1,2,3,4,5]
</code></pre>

<p>In fact, <code>[1,2,3]</code> is actually shorthand for <code>1:2:3:[]</code> (as pointed out in this <a href="http://learnyouahaskell.com/starting-out#an-intro-to-lists">tutorial I forgot to mention previously</a>).</p>

<p>We&#8217;re really dealing with a linked list here. A Haskell list consists of an initial element, called the <em>head</em>, with a link to the rest of the list, called the <em>tail</em>. There&#8217;s even built-in functions to help us get at these bits of the list:</p>

<pre><code>*Main&gt; let myList = [1,2,3,4,5]
*Main&gt; head myList
1
*Main&gt; tail myList
[2,3,4,5]
</code></pre>

<p>A <code>String</code> in Haskell is a list of characters, so we can do these same list operations on strings. The string <code>"hello"</code> is the same as <code>'h':'e':'l':'l':'o':[]</code>, so <code>tail "hello"</code> will return <code>"ello"</code></p>

<h2>Pattern matching</h2>

<p>I hope you&#8217;re still reading (unless you&#8217;ve gone off to read <a href="http://learnyouahaskell.com/">Learn You a Haskell</a> of course), because this is such a fun part of Haskell.</p>

<p>When defining a function in Haskell, we can actually provided different function bodies for different patterns of arguments. For example, let&#8217;s add this to our Haskell file:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">isZero</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Bool</span>
</span><span class='line'><span class="nf">isZero</span> <span class="mi">0</span> <span class="ow">=</span> <span class="kt">True</span>
</span><span class='line'><span class="nf">isZero</span> <span class="n">x</span> <span class="ow">=</span> <span class="kt">False</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<p>When we call this function, Haskell will check the function bodies from top to bottom, and evaluate the first one that matches the pattern for the specified arguments.</p>

<pre><code>*Main&gt; isZero 1
False
*Main&gt; isZero 432
False
*Main&gt; isZero 0
True
</code></pre>

<p>We can do much more exciting things than that with patterns though. We can tease apart data types like the lists we were working with earlier.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">isFirstItemZero</span> <span class="ow">::</span> <span class="p">[</span><span class="kt">Int</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Bool</span>
</span><span class='line'><span class="nf">isFirstItemZero</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">isZero</span> <span class="n">head</span>
</span><span class='line'><span class="nf">isFirstItemZero</span> <span class="n">x</span> <span class="ow">=</span> <span class="kt">False</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now if we call <code>isFirstItemZero [1,2,3]</code>, Haskell will check the first function body. Does <code>[1,2,3]</code> fit the pattern <code>(head:tail)</code>? Remembering that <code>[1,2,3]</code> is the same as <code>1:[2,3]</code>, we can see that it does. So we return <code>isZero 1</code>, which is <code>False</code>. By the same logic we get <code>isFirstItemZero [0,1,2]</code> equal to <code>True</code>.</p>

<p>What about <code>isFirstItemZero []</code>? We can&#8217;t break that into a head and a tail, so Haskell will check the next function body. Could <code>[]</code> go into a variable <code>x</code>? If we type <code>let x = []</code> into GHCi it doesn&#8217;t complain, so I guess it can. Haskell evaluates the second function body, so it returns <code>False</code>.</p>

<p>Haskell can work with some truly awesome patterns, but just to show you we&#8217;re not limited to single values and basic head/tail, let&#8217;s add this to our Haskell file:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">hasMoreThanOne</span> <span class="ow">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Bool</span>
</span><span class='line'><span class="nf">hasMoreThanOne</span> <span class="p">(</span><span class="n">first</span><span class="kt">:</span><span class="n">second</span><span class="kt">:</span><span class="kr">_</span><span class="p">)</span> <span class="ow">=</span> <span class="kt">True</span>
</span><span class='line'><span class="nf">hasMoreThanOne</span> <span class="kr">_</span> <span class="ow">=</span> <span class="kt">False</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will return <code>True</code> whenever the argument fits the pattern <code>(first:second:_)</code>. In Haskell, the underscore <code>_</code> matches anything, so all these will be <code>True</code>:</p>

<pre><code>*Main&gt; hasMoreThanOne (1:2:[])
True
*Main&gt; hasMoreThanOne (1:2:[3,4,5])
True
*Main&gt; hasMoreThanOne [1,2]
True
*Main&gt; hasMoreThanOne [1,2,3,4]
True
*Main&gt; hasMoreThanOne [1..]
True
</code></pre>

<p>Anything that doesn&#8217;t match that pattern, like <code>[1]</code> or <code>[]</code>, will fall through to the second function body and return <code>False</code>.</p>

<h2>Pattern matching with recursion</h2>

<p>Let&#8217;s try an example that uses recursion:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">len</span> <span class="ow">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">len</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">len</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">len</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="mi">0</span>
</span></code></pre></td></tr></table></div></figure>


<p>What happens when we call <code>len [1,2,3]</code>? Well, <code>[1,2,3]</code> gets split into <code>1:[2,3]</code>, so the function returns <code>1 + len [2,3]</code>.</p>

<p>So what&#8217;s <code>len [2,3]</code>? The <code>[2,3]</code> gets split into <code>2:[3]</code>, so that will return <code>1 + len [3]</code>. By the same logic <code>len [3]</code> returns <code>1 + len []</code>.</p>

<p>What&#8217;s <code>len []</code>? That doesn&#8217;t have a head and tail, so that falls through to the second method body, which returns <code>0</code>. So tracing through the whole function call, we get:</p>

<pre><code>len [1,2,3]
    = 1 + len [2,3]
    = 1 + (1 + len [3])
    = 1 + (1 + (1 + len []))
    = 1 + (1 + (1 + 0))
    = 3
</code></pre>

<h2>Pattern matching with case expressions</h2>

<p>If you find the multiple function bodies used for pattern matching confusing, you might find the case syntax easier to follow. In fact, according to <a href="http://learnyouahaskell.com/syntax-in-functions#case-expressions">Learn You a Haskell</a>, function pattern matching is really just syntactic sugar for case expressions anyway. So we can modify our previous <code>len</code> function like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">len</span> <span class="ow">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">len</span> <span class="n">x</span> <span class="ow">=</span>
</span><span class='line'>    <span class="kr">case</span> <span class="n">x</span> <span class="kr">of</span>
</span><span class='line'>        <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">len</span> <span class="n">tail</span>
</span><span class='line'>        <span class="kt">[]</span>          <span class="ow">-&gt;</span> <span class="mi">0</span>
</span></code></pre></td></tr></table></div></figure>


<p>I find this more confusing, so I&#8217;m not going to attempt to explain it, but I&#8217;d thought I&#8217;d mention it in case it helps you.</p>

<h2>Higher-order functions</h2>

<p>A higher-order function is one which takes one or more functions as arguments or returns a function (or both). In other words, it has a function in its type declaration. Functions passed to other functions are written with their type declarations surrounded by parentheses.</p>

<p>Let&#8217;s write a higher-order function called <code>incrementIf</code> that increments an integer only if another function returns <code>True</code> for that integer. So the function we&#8217;re passing through to <code>incrementIf</code> is going to have a type of <code>Int -&gt; Bool</code> &#8211; it will take an integer and return true or false, just like a <code>Func&lt;int, bool&gt;</code> in C# if you&#8217;re familiar with that. The second argument to <code>incrementIf</code> will be the integer it is working with, and finally <code>incrementIf</code> will return an integer.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">incrementIf</span> <span class="ow">::</span> <span class="p">(</span><span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Bool</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">incrementIf</span> <span class="n">predicate</span> <span class="n">x</span> <span class="ow">=</span> <span class="kr">if</span> <span class="n">predicate</span> <span class="n">x</span> <span class="kr">then</span> <span class="n">increment</span> <span class="n">x</span> <span class="kr">else</span> <span class="n">x</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here we have two arguments; the first one being a predicate function, the second being our integer. We&#8217;ve also used an <code>if-then-else</code> expression to return the incremented integer if the predicate is true, or else return the integer unchanged.</p>

<p>To test this we can use Haskell&#8217;s built in <code>even</code> function (or we could write it ourselves), which will return true or false depending on whether the integer is even.</p>

<pre><code>*Main&gt; incrementIf even 2
3
*Main&gt; incrementIf even 1
1
*Main&gt; incrementIf even 4
5
</code></pre>

<p>We can also declare simple functions in-line using lambda syntax. The syntax for lambdas follows the pattern <code>(\ firstArg secondArg ... -&gt; functionBody)</code> (the <code>\</code> is meant to represent the lambda symbol λ). So rather than using the built-in <code>even</code> function, we could pass our own using a lambda:</p>

<pre><code>*Main&gt; incrementIf (\x -&gt; x `mod` 2 == 0) 5
5
*Main&gt; incrementIf (\x -&gt; x `mod` 2 == 0) 6
7
</code></pre>

<h2><code>where</code> and <code>let</code></h2>

<p>The last bit of Haskell I used in my <a href="http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds.html">folds post</a> was a <code>where</code> binding. These can be used to pull out part of an expression for clarity, or to remove duplication if the same expression is used in multiple places within a single function. For example, these two functions are equivalent (the <code>'</code> mark is legal in Haskell function names):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">circumference</span> <span class="n">radius</span> <span class="ow">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">pi</span> <span class="o">*</span> <span class="n">radius</span>
</span><span class='line'>
</span><span class='line'><span class="nf">circumference&#39;</span> <span class="n">radius</span> <span class="ow">=</span> <span class="n">pi</span> <span class="o">*</span> <span class="n">diameter</span>
</span><span class='line'>    <span class="kr">where</span> <span class="n">diameter</span> <span class="ow">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">radius</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can also use a <code>let</code> binding for this, which goes before the function body rather than after.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">circumference&#39;&#39;</span> <span class="n">radius</span> <span class="ow">=</span>
</span><span class='line'>    <span class="kr">let</span> <span class="n">diameter</span> <span class="ow">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">radius</span>
</span><span class='line'>    <span class="kr">in</span>  <span class="n">pi</span> <span class="o">*</span> <span class="n">diameter</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>let</code> binding has some other properties (for example, you can use it in lots of places, such as typing <code>let a = 1</code> into GHCi), but check <a href="http://learnyouahaskell.com/syntax-in-functions#let-it-be">Learn You a Haskell</a> for more details.</p>

<h2>Putting it together</h2>

<p>Let&#8217;s apply a whole bunch of the things we&#8217;ve covered in this post by writing a function that filters a list. This function will have to take another function to do the filtering, which makes it a higher-order function. We&#8217;ll also use pattern matching and recursion to work our way through the list. We also want to support any type of list elements, so we&#8217;ll need a type signature that reflects that.</p>

<p>If we&#8217;re dealing with lists of any element type, let&#8217;s just call that type <code>a</code>. If we are filtering elements from a list of <code>a</code>, we&#8217;re going to end up with another list of <code>a</code>, so that will also be our return type. The predicate function we pass in to do the actual filtering on each element is going to have to take one of these <code>a</code>s, and return <code>True</code> or <code>False</code>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">filterList</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="kt">Bool</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now let&#8217;s try and implement it. We&#8217;re going to use pattern matching to break the list argument up into its head and tail. If the head element satisfies the predicate function, then well add that to the head of the result of filtering the tail. Otherwise we&#8217;ll drop that head element, and just return the result of filtering the tail. Finally, as a stopping condition for our recursion, we&#8217;ll tell Haskell that filtering the empty list will return the empty list, irrespective of what predicate function we pass in.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">filterList</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="kt">Bool</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span>
</span><span class='line'><span class="nf">filterList</span> <span class="n">f</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="kr">if</span> <span class="n">f</span> <span class="n">head</span> <span class="kr">then</span> <span class="n">head</span> <span class="kt">:</span> <span class="n">filteredTail</span> <span class="kr">else</span> <span class="n">filteredTail</span>
</span><span class='line'>    <span class="kr">where</span> <span class="n">filteredTail</span> <span class="ow">=</span> <span class="n">filterList</span> <span class="n">f</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">filterList</span> <span class="kr">_</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="kt">[]</span>
</span></code></pre></td></tr></table></div></figure>


<p>We&#8217;ve used <code>where</code> to pull out the <code>filteredTail</code> expression, otherwise we would have had to have <code>filterList f tail</code> in both the <code>if</code> and <code>else</code> conditions. This was just to try and make things neater, but we could have written it all in line too.</p>

<p>Now let&#8217;s use some declared functions and lambdas to filter a couple of lists:</p>

<pre><code>*Main&gt; filterList even [1..10]
[2,4,6,8,10]
*Main&gt; filterList odd [1..10]
[1,3,5,7,9]
*Main&gt; filterList isZero [-3..3]
[0]
*Main&gt; filterList (\x -&gt; x &gt; 10) [42, 7, 2, 109, 0, 15]
[42,109,15]
*Main&gt; filterList (\x -&gt; x `mod` 3 == 0) [1..20]
[3,6,9,12,15,18]
*Main&gt; filterList (\x -&gt; x &lt;= 'g') "Hello world, how are you today?"
"He d,  ae  da?"
</code></pre>

<p>If you&#8217;ve followed this whole <code>filterList</code> example then congratulations on making it successfully through the minefield that was my attempt at a Haskell quick start! Despite my best efforts, you&#8217;ve manage to learn a bit of Haskell. :)</p>

<h2>Where to from here?</h2>

<p>There is loads more to cover before I&#8217;d say we know introductory Haskell, particularly function composition, abstract data types (and pattern matching on those), typeclasses, IO, and then on to functors and <a href="http://davesquared.net/2011/08/functional-programming-newbie-and.html">monads</a>. The best way I know of to get this knowledge in a fun and attainable way is <a href="http://learnyouahaskell.com/">Learn You a Haskell</a>. But we&#8217;ve definitely made a start, and hopefully we&#8217;ve got enough to start deciphering code samples we see around the place.</p>

<p>Let me know if this helped you at all, or if you want me to go into something in greater detail, or if you&#8217;d like me to push further into some of the introductory stuff I&#8217;ve picked up so far.</p>

<p><a href="http://davesquared.net/downloads/code/haskell-misinformation.hs">Download the code: haskell-misinformation.hs</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Folds Pt 1: From recursion to folds]]></title>
    <link href="http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds.html"/>
    <updated>2012-02-05T00:08:00+11:00</updated>
    <id>http://davesquared.net/2012/02/folds-pt1-from-recursion-to-folds</id>
    <content type="html"><![CDATA[<p>I&#8217;ve recently been trying to <a href="http://learnyouahaskell.com/">learn some functional programming</a>, and one of the first things to trip me up was the idea of <em>folds</em>. Folds crop up all over the place in both functional and imperative languages, so they&#8217;re worth understanding. At their simplest, folds seem to be a short-hand for defining recursions over lists, but I find I start getting lost somewhere between fold types and optimising for languages with lazy evaluation.</p>

<p>This <a href="http://davesquared.net/categories/-folds">series on folds</a> is my attempt to pull together the little bits and pieces I&#8217;ve managed to pick up into a form I can understand. If you&#8217;re not familiar with folds, hopefully it will help get you started. If you already know about folds then you probably won&#8217;t get much out of this, but if you do read through it I&#8217;d love to get corrections via the comments or via email so I can update the post.</p>

<p>In this first post of the series we will try to work out what folds are. We&#8217;ll start by looking at some problems we can solve using recursion over lists. We&#8217;ll then try and work out what these solutions have in common, and factor that out. Finally we&#8217;ll see how this relates to folds, and how we can use folds to solve these problems more succinctly.</p>

<!--more-->




<div class="note"><b>Note:</b> I&#8217;ll use Haskell for this post but won&#8217;t assume you&#8217;ve had a chance to try it out, so I&#8217;ll try and explain all the relevant bits as we go. This also means that I&#8217;ll avoid some nice features of Haskell that could make the examples more concise and idiomatic, so we can focus less on the language, and more on the topic of folding. If you find yourself completely lost by the syntax, have a look at my attempt at a <a href="http://davesquared.net/2012/02/haskell-newbie-attempts-a-haskell-quick-start.html">Haskell quick start guide</a>.
</div>


<h2>Recursing over lists</h2>

<p>Let&#8217;s look at a Haskell function that uses recursion to return the length of a list.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">len</span> <span class="ow">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">len</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">len</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">len</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="mi">0</span>
</span></code></pre></td></tr></table></div></figure>


<p>The first line declares the type of our <code>len</code> function; it takes a list of any type <code>a</code> (expressed as <code>[a]</code>), and returns an <code>Int</code>.</p>

<p>The second line is where we define the length function in terms of recursion. Given the list argument, we&#8217;re going to use <a href="http://learnyouahaskell.com/syntax-in-functions#pattern-matching">pattern matching</a> to break that argument into its <code>head</code> (the first element of the list), and its <code>tail</code> (the rest of the list). For example: the head of the list <code>[1,2,3]</code> is <code>1</code> and its tail is <code>[2,3]</code>. When <code>len</code> is called with a list that has a head and tail, we&#8217;re going to return 1 plus the result of recursively calling <code>len</code> on the tail. We&#8217;ll see an example of this in a minute to make this a bit clearer if you&#8217;re lost in the syntax.</p>

<p>What happens when we&#8217;ve recursively called <code>len</code> and get to the end of the list? This is what the last line is for; it is the <em>stopping condition</em> for our recursion. If <code>len</code> is called for a list without a head and tail (i.e. the empty list), the pattern on the second line will not be matched and Haskell will look for the next <code>len</code> definition to see if that can provide a return value. Our third line can; it returns the length of the empty list <code>[]</code> as 0.</p>

<p>Let&#8217;s manually trace through what happens when we call this function:</p>

<pre><code>len [1,2,3]
    = 1 + len [2,3]
    = 1 + 1 + len [3]
    = 1 + 1 + 1 + len []
    = 1 + 1 + 1 + 0
    = 3
</code></pre>

<p>Looks reasonable to me. We&#8217;ve defined the length of a list as 1 for its first element (its head) plus the length of the rest (its tail). And when we have a list with no elements (the empty list), its length is 0.</p>

<h2>A pattern emerges</h2>

<p>What other list functions can we define using recursion? How about adding together all the numbers in a list?</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">add</span> <span class="ow">::</span> <span class="kt">Num</span> <span class="n">a</span> <span class="ow">=&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>  <span class="c1">--Take a list of numbers, return a number (all the elements added together)</span>
</span><span class='line'><span class="nf">add</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">head</span> <span class="o">+</span> <span class="n">add</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">add</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="mi">0</span>
</span><span class='line'><span class="cm">{- Results:</span>
</span><span class='line'><span class="cm">add [2,4,6]</span>
</span><span class='line'><span class="cm">    = 2 + add [4,6]</span>
</span><span class='line'><span class="cm">    = 2 + 4 + add [6]</span>
</span><span class='line'><span class="cm">    = 2 + 4 + 6 + add []</span>
</span><span class='line'><span class="cm">    = 2 + 4 + 6 + 0</span>
</span><span class='line'><span class="cm">    = 12</span>
</span><span class='line'><span class="cm">-}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This works very similarly to our <code>len</code> function, but instead of adding 1 to get the length, we&#8217;re adding the head value to get the sum of all the elements. Adding the elements of an empty list gives us 0. How about mapping a function over every element of the list?</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">mapFn</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span><span class="ow">-&gt;</span><span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span> <span class="c1">-- 1st arg is function that takes an &quot;a&quot; and returns a &quot;b&quot;,</span>
</span><span class='line'>                              <span class="c1">-- 2nd arg is a list of &quot;a&quot;,</span>
</span><span class='line'>                              <span class="c1">-- and we return a list of &quot;b&quot;.</span>
</span><span class='line'><span class="nf">mapFn</span> <span class="n">fn</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="p">(</span><span class="n">fn</span> <span class="n">head</span><span class="p">)</span> <span class="kt">:</span> <span class="n">mapFn</span> <span class="n">fn</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">mapFn</span> <span class="n">fn</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="kt">[]</span>
</span><span class='line'><span class="cm">{- Results:</span>
</span><span class='line'><span class="cm">mapFn (+1) [1,2]</span>
</span><span class='line'><span class="cm">    = (1+1) : mapFn (+1) [2]</span>
</span><span class='line'><span class="cm">    = (1+1) : (2+1) : mapFn (+1) []</span>
</span><span class='line'><span class="cm">    = (1+1) : (2+1) : []    --mapFn of [] is []</span>
</span><span class='line'><span class="cm">    = (1+1) : [3]</span>
</span><span class='line'><span class="cm">    = [2,3]</span>
</span><span class='line'><span class="cm">-}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In this example we&#8217;ve defined <code>mapFn</code> as a function <code>fn</code> applied to the head of the list (by calling <code>fn head</code>), then joined the result (using <code>:</code>) to the result of mapping <code>fn</code> over the rest of the list. We&#8217;ve also stated that mapping a function over an empty list returns an empty list.</p>

<p>All the functions we&#8217;ve seen follow a similar pattern. They operate over a list by splitting the list into head and tail, and return the result of doing <em>something</em> to the head and the result of recursively calling itself on tail. For <code>len</code>, the <em>something</em> was <code>1+</code>. For <code>add</code> it was <code>head +</code> and for <code>mapFn</code> it was applying <code>fn</code> to the head and joining to the rest of the result. And all of the functions have a value for the empty list to act as a stopping condition (returning 0 or <code>[]</code> in these cases).</p>

<h2>Eliminating the duplication</h2>

<p>As programmers we eschew duplication, so let&#8217;s introduce a function <code>f</code> that will remove the common bits of these functions, and instead let us focus on the important differences between them. What arguments will <code>f</code> need to take? This might end up sounding a bit confusing while we nut it out, but let&#8217;s push through it and see if it makes sense once we try and wire it all up at the end.</p>

<p>Our function will need to take a list of some type; all our previous functions have. As I don&#8217;t know exactly what type of elements will be in the list, let&#8217;s just call them type <code>a</code> as a place holder. We&#8217;ll also need to return a result, but what type should the return value be? For <code>len</code>, we used <code>Int</code>; it always returns an <code>Int</code>, even if it is working with a list of characters. So the result does not have to be the same type as the elements in the list; let&#8217;s just say it will return some type <code>b</code>.</p>

<p>We&#8217;ll also need some value to return for our stopping condition in the case of the empty list (for <code>len</code> this was 0). Now as we&#8217;ll be returning this value for the empty list, it will need to be the same type as our return value, which we called type <code>b</code>.</p>

<p>That&#8217;s most of the commonalities out of the way. What&#8217;s left is the <em>something</em> we do to the head of the list and the result of recursively calling on the tail. That sounds like a function definition to me; it takes the head of our list of <code>a</code>s, and the result of calling on the tail (we called the result type <code>b</code>), and returns the final result (also type <code>b</code>).</p>

<p>In Haskell-speak, we now have our function declaration as <code>f :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b</code>. The first argument is the function that does <em>something</em> with the head and the result of the recursive call with the tail. The second argument is the value we want to use when our list is empty. The third argument is the list of <code>a</code>s we&#8217;re recursing over. And finally, we&#8217;re returning some value of type <code>b</code>.</p>

<p>If you&#8217;re feeling a bit lost then that makes two of us. Let&#8217;s try and implement it based on what we know and hope for the best. :)</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">f</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">b</span>
</span><span class='line'><span class="nf">f</span> <span class="n">func</span> <span class="n">valueWhenEmpty</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">func</span> <span class="n">head</span> <span class="p">(</span><span class="n">f</span> <span class="n">func</span> <span class="n">valueWhenEmpty</span> <span class="n">tail</span><span class="p">)</span>
</span><span class='line'><span class="nf">f</span> <span class="n">func</span> <span class="n">valueWhenEmpty</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="n">valueWhenEmpty</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Wat?</h2>

<p>If you&#8217;re like me then you&#8217;re probably thinking our <code>f</code> looks like <a href="http://en.wikipedia.org/wiki/Gobbledygook">gobbledegook</a>. Let&#8217;s start by looking at the familiar pieces. The last line has our stopping condition for the empty list <code>[]</code>; it just returns the required value when the list is empty. Line 2 has our trusty <code>(head:tail)</code> pattern on the left-hand side. What&#8217;s the right-hand side doing?</p>

<p>Remember, the first argument (<code>func</code>) is a function that is going to do <em>something</em> with the head of the list and the result of recursively calling on the tail. The <code>f func valueWhenEmpty tail</code> is our recursive call with the tail. If it helps, we could pull out that part of the statement and rewrite the second line like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">f</span> <span class="n">func</span> <span class="n">valueWhenEmpty</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="n">func</span> <span class="n">head</span> <span class="n">recursiveCallWithTail</span>
</span><span class='line'>    <span class="kr">where</span> <span class="n">recursiveCallWithTail</span> <span class="ow">=</span> <span class="n">f</span> <span class="n">func</span> <span class="n">valueWhenEmpty</span> <span class="n">tail</span>
</span></code></pre></td></tr></table></div></figure>


<p>Let&#8217;s try and apply this to something we already know &#8211; our trusty old <code>len</code> function. If we&#8217;ve extracted out the common bits of the recursion we should be able to express <code>len</code> in terms of <code>f</code>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="c1">-- original</span>
</span><span class='line'><span class="nf">len</span> <span class="ow">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">len</span> <span class="p">(</span><span class="n">head</span><span class="kt">:</span><span class="n">tail</span><span class="p">)</span> <span class="ow">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">len</span> <span class="n">tail</span>
</span><span class='line'><span class="nf">len</span> <span class="kt">[]</span> <span class="ow">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- new</span>
</span><span class='line'><span class="nf">len2</span> <span class="ow">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="kt">Int</span>
</span><span class='line'><span class="nf">len2</span> <span class="n">list</span> <span class="ow">=</span> <span class="n">f</span> <span class="n">func</span> <span class="mi">0</span> <span class="n">list</span>
</span><span class='line'>    <span class="kr">where</span> <span class="n">func</span> <span class="n">head</span> <span class="n">lenOfTail</span> <span class="ow">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">lenOfTail</span>
</span></code></pre></td></tr></table></div></figure>


<p>I think I&#8217;m starting to see how this hangs together now. Our <code>func</code> takes as arguments the head of the list, and the result of recursively calling with tail (which in this case gives the length of the tail). This returns <code>1 + lenOfTail</code>, which is the same as <code>1 + len tail</code> from the original <code>len</code> function. We&#8217;re also passing in <code>0</code> for our empty list value, which gives us the same as the <code>len [] = 0</code> from the original example.</p>

<p>Let&#8217;s step through the evaluation of each function:</p>

<pre><code>len [1,2,3]
    = 1 + len [2,3]
    = 1 + (1 + len [3])
    = 1 + (1 + (1 + len []))
    = 1 + (1 + (1 + 0))
    = 3

len2 [1,2,3]
    = f func 0 [1,2,3]
    = 1 + (f func 0 [2,3])
    = 1 + (1 + (f func 0 [3]))
    = 1 + (1 + (1+ (f func 0 [])))
    = 1 + (1 + (1+0))
    = 3
</code></pre>

<p>Here we can see that both <code>len</code> and <code>len2</code> work exactly the same way, it&#8217;s just that <code>len2</code> is now going via a function that handles the recursion plumbing for us.</p>

<h2>Folding</h2>

<p>As you may have guessed, our <code>f</code> function is a <em>fold</em> (more specifically, a right fold, which we&#8217;ll get to in a later post). We&#8217;re <em>folding</em> the <code>func</code> argument over a list and providing a particular value for the stopping condition.</p>

<p>What I&#8217;ve been clumsily referring to as &#8220;the result of recursively calling the function on the tail&#8221; tends to be known as the <em>accumulator</em>, because it represents the accumulation of the results for each element in the tail. The empty list value is known as the <em>seed</em>, as that ends up being the first value of the accumulator once we get to the bottom of the recursion and start working out way back up. Fold itself can also be known as <a href="http://railspikes.com/2008/8/11/understanding-map-and-reduce">inject or reduce</a>, or <a href="http://msdn.microsoft.com/en-us/library/bb549218.aspx">Aggregate in .NET</a>.</p>

<p>Let&#8217;s quickly express our other examples using our fold function (renamed from <code>f</code>):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">add2</span> <span class="ow">::</span> <span class="kt">Num</span> <span class="n">a</span> <span class="ow">=&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="n">a</span>
</span><span class='line'><span class="nf">add2</span> <span class="n">list</span> <span class="ow">=</span> <span class="n">fold</span> <span class="p">(</span><span class="o">+</span><span class="p">)</span> <span class="mi">0</span> <span class="n">list</span>
</span><span class='line'>
</span><span class='line'><span class="nf">mapFn2</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span><span class="ow">-&gt;</span><span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="n">b</span><span class="p">]</span>
</span><span class='line'><span class="nf">mapFn2</span> <span class="n">fn</span> <span class="n">list</span> <span class="ow">=</span> <span class="n">fold</span> <span class="p">(</span><span class="nf">\</span><span class="n">element</span> <span class="n">acc</span> <span class="ow">-&gt;</span> <span class="p">(</span><span class="n">fn</span> <span class="n">element</span><span class="p">)</span> <span class="kt">:</span> <span class="n">acc</span><span class="p">)</span> <span class="kt">[]</span> <span class="n">list</span>
</span><span class='line'>  <span class="c1">-- Here (\a b -&gt; retVal) is a lambda expression that takes 2 arguments.</span>
</span><span class='line'>  <span class="c1">-- Think Func&lt;A, B, B&gt; in C#.</span>
</span></code></pre></td></tr></table></div></figure>


<h2>But why?!?!</h2>

<p>Because it gives us a way of expressing functions that work over lists without the noise of the recursion mechanics getting in the way.</p>

<p>At first the fold versions may seem confusing compared to explicit recursion, but after gaining some familiarity with the steps, folds start to let us immediately focus on the intent of the code. Our function becomes a statement of the absolute essence of the problem we&#8217;re solving. The <code>add2</code> example shows us folding <code>+</code> over a list, starting with a seed of 0. The essence of the function is adding, and there&#8217;s the <code>(+)</code> function sitting first and foremost in the call to <code>fold</code>.</p>

<p>Once we start using some Haskell niceties like partial application and function composition we can start getting some very concise, elegant function definitions, expressed in terms of other functions.</p>

<h2>Still to come&#8230;</h2>

<p>In the <a href="http://davesquared.net/2012/02/folds-pt2-from-loops-to-folds.html">next post</a> we&#8217;ll look at the different types of folds. After that we&#8217;ll move on to look at some of the runtime characteristics of folds (and what we can do about them :)).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Goodbye Blogger]]></title>
    <link href="http://davesquared.net/2012/01/goodbye-blogger.html"/>
    <updated>2012-01-29T21:28:00+11:00</updated>
    <id>http://davesquared.net/2012/01/goodbye-blogger</id>
    <content type="html"><![CDATA[<p>After blogging for five years on <a href="http://www.blogger.com">Blogger</a>, I&#8217;ve finally decided to take the plunge and run my own blog. I&#8217;ve actually really enjoyed hosting with Blogger; I think it&#8217;s a pretty under-rated platform. It&#8217;s easy to get started, possible to do all sorts of crazy customisations, and they&#8217;ve recently started adding some cool stuff like mobile templates and <a href="http://buzz.blogger.com/2011/03/fresh-new-perspectives-for-your-blog.html">different dynamic views</a>. So why on earth am I leaving again?</p>

<!--more-->


<p>Well, a couple of reasons. First, I like to write posts in <a href="http://en.wikipedia.org/wiki/Markdown">Markdown</a> using Vim. For the last year or so I&#8217;ve been converting posts to HTML, piping the output to clipboard, and pasting it into Blogger&#8217;s web UI. Switching to a platform that supports Markdown makes that a little simpler. I&#8217;ve also never really like the Blogger/Picasa Web image integration thing, so running my own blog means I can dump images wherever I want to and embed away. I also wanted to experiment a bit more with different layouts, but rather than relying on Blogger&#8217;s templating system, I wanted to work with straight HTML and CSS (well, via <a href="http://en.wikipedia.org/wiki/Sass_(stylesheet_language">SASS</a>). This would also buy me separation between layout, content, and platform.</p>

<p>If all this sounds a bit tenuous to you then you are probably right. I think all this is justification for me just wanting a change after half a decade on the same platform.</p>

<p>And so you are now reading a static site generated by <a href="http://octopress.org/">Octopress</a>, hosted on <a href="http://www.heroku.com/">Heroku</a>. I&#8217;ve imported everything over from Blogger and for the most part have kept the same links to avoid breaking stuff. If you find anything amiss or have any problems reading on your browser / feed / device please let me know and I&#8217;ll do my best to fix it up (yes, I know the font is large :)).</p>

<p>Thanks to <a href="https://twitter.com/aeoth">Paul</a> for good naturedly goading me into finally flicking over to Octopress and for helping me out with some questions; <a href="http://twitter.com/troyhunt">Troy</a>, <a href="http://twitter.com/thecolonial">OJ</a> and <a href="https://twitter.com/tarnacious">Tarn</a> for their advice; and <a href="https://twitter.com/thomasjo">Thomas</a> for putting up with all sorts of inane questions I bombarded him with over email and IM.</p>

<p>And finally, thanks to Blogger for an enjoyable 5 years! So long, and thanks for all the posts and fish and stuff. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What do you enjoy about programming?]]></title>
    <link href="http://davesquared.net/2011/12/what-do-you-enjoy-about-programming.html"/>
    <updated>2011-12-16T20:17:00+11:00</updated>
    <id>http://davesquared.net/2011/12/what-do-you-enjoy-about-programming</id>
    <content type="html"><![CDATA[<p>Over the last couple of weeks I think I&#8217;ve identified one of the main things I enjoy about programming. For me, it seems to be the puzzle-like aspect of it; finding ways to slot small bits and pieces together to create/achieve something bigger. When I&#8217;m working on a problem I can approach in this way I find it much easier to get into a good <a href="http://en.wikipedia.org/wiki/Flow_(psychology)">flow</a> where I&#8217;m productive, creative and happy.</p>

<p>This idea started to dawn on me while I was in <a href="https://twitter.com/joshuakerievsky">Joshua Kerievsky&#8217;s</a> Refactoring workshop at <a href="http://www.yowconference.com.au/">YOW 2011</a>. We were going through refactoring exercises, but trying to take extremely small steps to keep the tests green. The final goal could be achieved fairly easily by moving a few chunks of code around while keeping the tests broken for a while, but I really enjoyed the challenge of finding small steps that would keep the tests passing all the way to the final state.</p>

<p>This was further reinforced at <a href="https://twitter.com/dibblego">Tony&#8217;s</a> excellent Functional Programming workshop, where we spent quite a bit of time trying to fit together various functions (and partial applications of them) according to their type signatures. I thoroughly enjoyed this process, despite really struggling to get the solutions much of the time. I also realised I get an extra kick out of these puzzles when there is an element of elegance or simplicity to the way these pieces fit together. For example, implementing <code>map</code> with folds rather than recursion and pattern matching.</p>

<p>I think identifying what you enjoy about programming (or any activity really) can be an important part of maintaining motivation, and improving productivity and creativity as a result. I&#8217;m going to put this idea to the test by trying to break down problems in ways that I can use approaches I enjoy, and see if that helps me get better results, both in terms of solution and my happiness and enthusiasm.</p>

<p>This knowledge also helps me better understand some of my weaknesses. I have spent a lot of time over the years trying to piece together various patterns and practices to try and find a process I can continually apply to create great software. In other words, trying to solve the puzzle of how to develop software. My propensity to approach things as puzzles has probably led me astray here, as the unlikelihood of a silver bullet for software development has been demonstrated time and time again. Recognising this, I&#8217;ll try be more careful to make sure I&#8217;m solving the right problems, and not just inventing problems to puzzle-solve (although that can be educational as well, provided it&#8217;s deliberate).</p>

<p>So, what is it about programming you enjoy? Can you use that knowledge to improve the way you work?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dave's not so excellent typing adventure]]></title>
    <link href="http://davesquared.net/2011/12/daves-not-so-excellent-typing-adventure.html"/>
    <updated>2011-12-13T17:40:00+11:00</updated>
    <id>http://davesquared.net/2011/12/daves-not-so-excellent-typing-adventure</id>
    <content type="html"><![CDATA[<p>A few weeks ago I decided to depart from my usual QWERTY use and try an alternate keyboard layout. Rather than go the moderately rare <a href="http://en.wikipedia.org/wiki/Dvorak_Simplified_Keyboard">Dvorak</a> layout, I decided to go full-hipster and try <a href="http://en.wikipedia.org/wiki/Colemak#Colemak">Colemak</a>. This post describes the ensuing hilarity and (mis)adventure.</p>

<h2>Why not QWERTY?</h2>

<p>I type reasonably well with QWERTY: normally between 90 - 110 wpm. I started doing some practice to try and improve my accuracy and technique and noticed a few things. First, my right hand tended to drift all over the keyboard (away from the home position), which would result in me sometimes getting lost and making a mistake. A mistake at 110 wpm means typing around 10 characters in the second it takes you to realise it. Backing out those characters, correcting the mistake, correcting your positioning, and continuing gives your typing speed a pretty big hit and is quite frustrating.</p>

<p>Second, my hands had to do all sorts of gymnastics while typing. Common English character combinations like &#8216;st&#8217;, &#8216;ion&#8217;, &#8216;ce&#8217;, &#8216;ed&#8217; require some big shifts that take you away from the home position (slow and error prone), or require using the same finger for consecutive characters (slow and encourages shortcuts which shift your position).</p>

<p>Third, QWERTY is left-hand biased (at least for English). This can become quite fatiguing. There are also words like &#8216;minimum&#8217; and the frequently-typed &#8216;sweaterdresses&#8217; (pointed out on the <a href="http://colemak.com/wiki/index.php?title=Ergonomic">Colemak site</a>) that rely on a single hand; again, quite fatiguing. </p>

<p>I started to wonder if improving my QWERTY was worthwhile. Maybe I should try an alternate keyboard layout like Dvorak, which was meant to address many of these comfort and accuracy issues (my main goal), and also improve overall speed (a distant second goal).</p>

<h2>Why Colemak?</h2>

<p>It was around this time I saw <a href="http://twitter.com/thecolonial">@TheColonial</a> mention something about the Colemak layout. I&#8217;d never heard of it, but in trying to research what it was I found some promising signs. Colemak was designed to be efficient and ergonomic by keeping commonly used keys on the home row and on the strongest fingers (check out the <a href="http://colemak.com/forum/viewtopic.php?id=128">heatmaps</a> for Colemak et al.), as well as being easy to learn by not deviating too far from QWERTY (the entire bottom left hand is unchanged from QWERTY, which means common shortcuts like Ctrl+Z/X/C/V are unchanged).</p>

<p>If you&#8217;ve ever experienced <a href="http://www.hanselman.com/blog/TheProgrammersHands.aspx">programmer hands</a>, you&#8217;ll appreciate why it is important to look after yourself and avoid RSI. I happened across <a href="http://patorjk.com/keyboard-layout-analyzer/">PAT or JK&#8217;s keyboard layout analyser</a>, plugged in a few pieces of code and blog posts, and found that, had I typed these using Colemak instead of QWERTY, my fingers would have moved in the order of 50% less to type the same text. Figures like 192m for QWERTY, 97m for Colemak were not uncommon. It also tended to beat out Dvorak. How could anyone interested in efficiency <em>not</em> try that out?</p>

<h2>Three weeks with Colemak</h2>

<p>I started out doing Colemak exercises with <a href="http://web.me.com/typetrainer4mac/aTypeTrainer4Mac/home.html">aTypeTrainerForMac</a>. Largely due to its commonalities with QWERTY, the layout itself was quite easy to learn. I had the key positions memorised by the end of the first day, but using it effectively was another matter entirely. </p>

<p>No matter how well I knew the individual keys, I found my brain wanted to think about typing a whole word or pattern at a time. I could start typing the first few characters in Colemak, get to a familiar pattern like &#8216;ion&#8217;, and my brain would leap ahead and get me to complete the word (incorrectly) in QWERTY. Getting my reason to fight these reflexes was very uncomfortable, but at the same time a fascinating insight into how the brain works.</p>

<p>After 1 week of fairly intense practice, I could type steadily at 20 wpm with Colemak, but my QWERTY had dropped a bit to 80.</p>

<p>Shortly after that (with the help of autocomplete) I was able to use Colemak pretty effectively. After three weeks of almost exclusive use I could type between 40-50 wpm and it felt very comfortable. My technique and accuracy were improving. </p>

<p>It was then I found my QWERTY was down to 15 wpm. That is not a typo; a measly 15 wpm. I was completely unable to use a standard keyboard.</p>

<h2>Return of the QWERTY</h2>

<p>I was a few days away from attending a Code Retreat in Brisbane, and was really concerned about being able to pair with people when I couldn&#8217;t type properly. I decided to jump back into QWERTY full time. It took a solid day, but after being completely useless for an hour or so the reflexes started to come back. By the end of the day I was back to 90 wpm (provided I didn&#8217;t think too hard about it :)). Surprisingly I could still type ~30 wpm on Colemak.</p>

<p>One thing that really surprised me is how clumsy QWERTY felt to me. I could feel the added strain on my fingers, wrists and shoulders due to the increased movement. Accuracy seemed to come much easier with Colemak, although that could have been a product of how I had trained myself up on all the characters, rather than on real text. I also really started to notice the bad touch typing habits I had formed with QWERTY, like leaving the home row and using wrong fingers to hit keys due to the key positions of many words.</p>

<h2>Current state</h2>

<p>I&#8217;m really undecided on how to proceed from here. One one hand Colemak really seems a lot more comfortable, and I&#8217;m pretty confident I could get up to comparable levels of speed with it.</p>

<p>On the other hand, almost <em>every other keyboard in the English-speaking world uses QWERTY</em>. It&#8217;s obviously going to take me a bit of work to maintain proficiency with both QWERTY and Colemak. The alternative, being unable to use most other computers effectively, is very unappealing. If I was only ever going to use my own computer I&#8217;d almost definitely stick with Colemak, but as I do a lot of pair programming it really comes down to exactly how much effort it&#8217;s going to take to be bi-typal. </p>

<p>If we end up getting <a href="https://sermoa.wordpress.com/2011/11/27/qido-for-colemak-please/">QIDO for Colemak</a> it may help for pair programming, but I&#8217;m still concerned about being stuck at some random computer and being unable to use it.</p>

<p>My other concern is that my perceived increase in comfort with Colemak is at half the speed of my QWERTY typing. I&#8217;m not sure whether Colemak would still be comfortable once I hit 100 wpm, or whether I would get the same discomfort as I do with QWERTY.</p>

<p>So there you have it; a bit of a non-conclusion. Colemak seems to have a lot of things going for it, but it&#8217;s hard to recommend it in the face of the ubiquity of QWERTY and with only anecdotal evidence of it&#8217;s ergonomic benefits (and there&#8217;s some <a href="http://ergonomicinfo.com/articles/colemak-dvorak/">counter-anecdotes</a> too). It&#8217;s definitely an interesting experiment to try, just for some insight in to how your brain works with typing, but unless you only use your own hardware I&#8217;d suggest keeping up speed on QWERTY as well.</p>

<p>As for me, I think I&#8217;ll try and develop both QWERTY and Colemak in parallel for a while and see what happens.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Documentation via automation]]></title>
    <link href="http://davesquared.net/2011/11/documentation-via-automation.html"/>
    <updated>2011-11-26T22:01:00+11:00</updated>
    <id>http://davesquared.net/2011/11/documentation-via-automation</id>
    <content type="html"><![CDATA[<p>There are lots of benefits to automating common, recurring tasks like builds and deployments. We gain reliable, repeatable results and remove the cost of duplicated effort. However I&#8217;ve just come across a related benefit that makes it worthwhile considering automation for less common tasks, and that&#8217;s as a form of reliable documentation.</p>

<p>I&#8217;m currently battling some driver signing issues. We have some incomplete documentation from a previous occasion we&#8217;ve been through a related problem, but no one remembers the exact steps used. Because this previous occasion was reasonably considered a one-off, it was never automated. If it had been, it would now provide reliable, repeatable, unambiguously documented steps for getting me out of my current jam. Even if not an exact match for my current requirements, having a working example I could use as a basis to work from would be invaluable.</p>

<p>This has made me think that it is probably worth automating a whole bunch of tasks I hadn&#8217;t previously considered due to their infrequent nature. What better documentation of a procedure than working, executable steps? Sure, there may be a need for accompanying rationale, but given a blind choice between a Word/wiki document and something that actually runs and works, I&#8217;m picking the latter.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Some mocking opinions]]></title>
    <link href="http://davesquared.net/2011/11/some-mocking-opinions.html"/>
    <updated>2011-11-22T22:25:00+11:00</updated>
    <id>http://davesquared.net/2011/11/some-mocking-opinions</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been thinking through how I use test doubles (mocks, stubs, test spies, etc) recently, and thought I&#8217;d write down a snapshot of my current opinions on the subject.</p>

<h2>Don&#8217;t mock types you don&#8217;t own</h2>

<p>I&#8217;ve <a href="http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html">written about this before</a>, and I still think it is good advice. Test down to your lowest level of abstraction, then integration / contract / acceptance test over the boundary. By mocking a type you don&#8217;t own that dependency starts bleeding in to your code and pushing your own abstractions in potentially unhelpful ways. You&#8217;re also not really testing much; unless you also have good contract tests then checking you&#8217;ve called a specific external method is not going to tell you much about whether your code does what it needs to.</p>

<p>Contrived example: say I was faking out <code>Math.Round()</code> (let&#8217;s pretend it&#8217;s an instance method or you are using a framework that can mock statics via the profiler API):</p>

<pre><code>[Test]
public void Calculate_with_rounding() {
    math.Round(2.5).Returns(3); //Fake math.Round
    var subject = new Calculation(math);

    Assert.AreEqual(subject.AddAndRound(1, 1.5), 3);
}
</code></pre>

<p>Perfectly reasonable? Well, except for the fact .NET uses banker&#8217;s rounding and rounds <a href="http://stackoverflow.com/questions/977796/in-c-math-round2-5-result-is-2-instead-of-3-are-you-kidding-me">2.5 to 2</a> (and 1.5 to 2 for that matter). If you were using Python (which rounds away from 0), you would have been spot on. If you care about the rounding method, you&#8217;ve now got a bug.</p>

<p>If something as simple as rounding can trip us up, imagine what we could do if we start mocking ORM or ADO.NET calls.</p>

<h2>Try to avoid mocks in acceptance tests</h2>

<p>In my experience this tends to result in too much behaviour being pushed into the mocks. My first preference is to use real pieces, second is to hand-code fakes that have enough logic to work as required, and convenience methods to help tests configure them appropriately. As per &quot;don&#8217;t mock types you don&#8217;t own&quot;, it is also a good idea to test your fakes match the real behaviour.</p>

<h2>Learn mocking before a mocking framework</h2>

<p>I&#8217;ve often heard developers new to automated testing say things like &quot;I really need to learn (Rhino Mocks | Moq | Mocking Framework X)&quot;. I think this is the wrong emphasis; before learning a framework for creating test doubles it&#8217;s important to understand how test doubles work and how to use them.</p>

<p>For me, a great way to learn was to hand-roll all my fake objects for my tests to act as required. Manually stubbing out values and/or recording calls gave me a good understanding of the different types of test doubles (mocks, spies, stubs etc.) and how they work. Once this got old (very quickly) it was fairly simple to take the behaviour I knew how to hand-code and translate that into the syntax required by a mocking framework. It just became a matter of automating what I was already doing. (It also helped me understand common difficulties like trying to mock non-virtual members.)</p>

<h2>Don&#8217;t explictly test intermediate steps or inconsequential details</h2>

<p>If we assert on details of an implementation we tend to get tight coupling and brittle tests. An example I have seen fairly frequently is:</p>

<pre><code>[Test]
public void Should_get_the_widget_from_the_factory() {
    var factory = MockRepository.GenerateMock&lt;IWidgetFactory&gt;();
    var subject = new Foo(factory);
    subject.DoStuff();
    factory.AssertWasCalled(x =&gt; x.GetWidget());
}

[Test]
public void Should_turn_the_widget() {
    var widget = MockRepository.GenerateMock&lt;IWidget&gt;();
    var factory = MockRepository.GenerateStub&lt;IWidgetFactory&gt;();
    factory.Stub(x =&gt; x.GetWidget()).Return(widget);
    var subject = new Foo(factory);
    subject.DoStuff();
    widget.AssertWasCalled(x =&gt; x.Turn());
}
</code></pre>

<p>Here the first test is a completely redundant. The second test covers that entire code path (how else could the widget from the factory get turned, if the subject did not call the factory?). Now you could argue that you prefer the extra, explicit specification the first test provides, to which I&#8217;d respond that I don&#8217;t think it&#8217;s worth the pain from the additional friction it causes when you want to change this implementation detail. </p>

<p>Besides, what do we really care about for our subject? That it uses a factory? Or that it turns the widget? Focus on how you want the object to behave, not how it implements that behaviour.</p>

<p>This approach can help lead us to better abstractions, as we start identifying roles and responsibilities separately from implementation details. And it will definitely make your code easier to change without the friction of over-specified tests.</p>

<h2>Mock interaction with the contract, not the specific implementation</h2>

<p>On a highly related point, the aim of abstraction is decoupling from the implementation. If we are configuring our test doubles with lots of behaviour that our unit tests are relying on then our object is coupled to that particular implementation, not to a contract of behaviour or a role. For an abstraction to be effective we should be able to drop in a completely new implementation that fulfils the required role. This is not the case if we need to set up a test double&#8217;s method to call another dependency and return some rearrangement of the result. If we&#8217;re relying on that in our test then our abstraction has failed.</p>

<p>Sometimes you just get stuck with having to perform a callback from a stub, but in general if you are pushing behaviour into your mock, re-think the design or consider using a hand-coded fake before you go contorting your mocking framework.</p>

<h2>Beware over-abstraction</h2>

<p>It is quite easy to churn out layers of useless abstractions when using mock frameworks. Abstractions have a cost. Feedback from tests is great, but pay close attention to SOLID and the rules of simple design and call out to meaningful abstractions, rather than putting in a dependency just for testing. I wrote some <a href="http://davesquared.net/2011/06/abstraction-and-oo.html">guidelines on abstractions</a> a while back that I don&#8217;t entirely disagree with yet.</p>

<h2>Some unanswered questions</h2>

<h3>Mocks vs. stubs, tell vs. ask.</h3>

<p>I&#8217;ve tended to prefer stubs over mocks (stubbing out the results of calls rather than checking they were received), as per the widget factory example above. This flies in the face of the &quot;Tell, don&#8217;t ask&quot; principle, which recommends we don&#8217;t ask a collaborator for some state and act on the result, but instead give the collaborator the state it needs from us and tell it what to do with it. This seems to suggest I should be using mocks (checking received calls) a whole lot more than I stub them out.</p>

<h3>Avoiding &quot;Yet Another Factory&quot;</h3>

<p>If an object news up something, our unit test will typically have almost no ability to affect that object. If we want to check our subject news up a view model and calls <code>Activate()</code> on it, we have no way of asserting this was done without exposing <code>IsActivated</code> and relying on that implementation detail. Leaky abstraction. Bad.</p>

<p>One solution is injecting a factory into our object. We can stub out what this returns, make it return another test double, and then check it received the <code>Activate()</code> call. Just introduce a factory. Yet another factory. Searching through files matching <code>*Factory</code> becomes an exercise akin to reading War and Peace. And they&#8217;re generally not even <a href="http://en.wikipedia.org/wiki/Factory_pattern">real factories</a>! They don&#8217;t choose a particular implementation, they are just a glorified wrapper over a single constructor.</p>

<p>Sure, I sometimes try to ease my conscience by injecting a factory method as a <code>Func&lt;T&gt;</code> which my IoC container helps me with. But deep down I know its still YAF, and a small part of me dies.</p>

<p>I&#8217;m hoping choosing &quot;better&quot; abstractions will help me with this, but I&#8217;ve had limited success to date.</p>

<h3>Interface explosion</h3>

<p>C# seems to make it difficult to do testing without using interfaces. And so I end up pulling out yet another interface that will never see another implementation. I&#8217;ve heard <a href="https://twitter.com/shannoncornish">Shannon</a> refer to them as &quot;the new header files&quot;. Every class has its interface documented in its header/interface file. It&#8217;s easy to say just choose better abstractions where the interface can be reused, but this is still something I struggle with.</p>

<h3>Mocking in dynamic languages</h3>

<p>This post has been written primarily from the perspective of static languages; I not sure how much (if any) applies to dynamic languages. From my limited experience testing and mocking seem to be done quite differently in languages like Ruby and Python. I&#8217;m keen to learn more about how mocking is done in these languages and see how much can help me improve how I test.</p>

<h2>End of transmission</h2>

<p>This has been a brain dump of my current opinions about mocking. If you agree, disagree, and/or can help me with some of my unanswered questions, please leave a comment.</p>

<p>Now if you&#8217;ll excuse me, I&#8217;m off to code up yet another factory&#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Odd problem with Git, Windows and virus-checkers]]></title>
    <link href="http://davesquared.net/2011/10/odd-problem-with-git-windows-and-virus.html"/>
    <updated>2011-10-22T22:19:00+11:00</updated>
    <id>http://davesquared.net/2011/10/odd-problem-with-git-windows-and-virus</id>
    <content type="html"><![CDATA[<p>Had a really odd git problem this week, with an even odder solution, so am posting in the hope of helping the next poor dev who has to try and track this down via Google.</p>

<p>We had a branch checked out with 2 new commits on it, and we wanted to squash it into a single commit using <code>git rebase -i (basecommit)</code>. This would start the normal interactive rebase, then get into a loop of printing the following error to console:</p>

<pre><code>mv: cannot move '.git/rebase-merge/git-rebase-todo.new' to '.git/rebase-merge/git-rebase.todo'
</code></pre>

<p>Looking at the <code>.git/rebase-merge</code> folder, I could see the <code>git-rebase-todo.new</code> file getting repeatedly created, then deleted. This was happening on two different machines.</p>

<p>Some googling lead me to <a href="http://osdir.com/ml/msysgit/2010-01/msg00007.html">this post</a> which suggested a virus checker may be locking the file.</p>

<p>Sure enough, turning off Microsoft Security Essentials&#8217; Real-time protection, doing the rebase, then turning it back on again, resolved the problem. I&#8217;ve never had this problem before so must have just gotten &#8220;lucky&#8221; with this particular file matching some property the virus checker was looking for.</p>

<p>I guess this is probably worth trying whenever getting strange file IO errors from any software ported to Windows.</p>
]]></content>
  </entry>
  
</feed>

