<p>Haskell is statically typed language, meaning that during compilation, programs are checked for type correctness. This means that you won’t accidentally mix for example text and numbers. Haskell does type inference. The compiler will try and figure out what kind of types would make your program to be valid in terms of types. Programmer could completely omit types, but it’s often helpful to write type signatures for at least top level definitions. These will be helpful for both the programmers and compilers.</p>
<h2 id="concrete-types">concrete types</h2>
<p>Simplest case is where types are spelled out definitely. Function <code>add</code> below takes two <code>Integer</code> parameters and produces <code>Integer</code> value. Note that types are written in upper case.</p>
<p><code>add :: Integer -&gt; Integer -&gt; Integer</code></p>
<p>It’s possible to not use concrete types. In following example <code>a</code> (note the lower case) can be anything. So function takes two values of <code>a</code>, a <code>Boolea</code> and produces <code>a</code>. This is useful technique for writing very general functions.</p>
<p><code>choose :: a -&gt; a -&gt; Boolean -&gt; a</code></p>
<h2 id="ad-hoc-polymorphism">ad hoc polymorphism</h2>
<p>In previous example, we wouldn’t be able to do much at all with <code>a</code> as we don’t know its type. Sometimes we need to know a bit more about type, without specifically declaring its type. For those cases type constraints are useful.</p>
<p><code>add :: (Num a) =&gt; a -&gt; a -&gt; a</code></p>
<p>This version of <code>add</code> again takes two parameters, both being type <code>a</code> and produces value <code>a</code>. But <code>(Num a) =&gt;</code> part in the signature constraints <code>a</code> to be instance of <code>Num</code>. This type class (I’ll talk about these some other time) defines that each instance of it will have set of functions: <code>+</code>, <code>-</code>, <code>*</code>, <code>negate</code>, <code>abs</code>, <code>signum</code> and <code>fromInteger</code>. So now our <code>add</code> function can use those functions, regardless of what specific type <code>a</code> is.</p>
<h2 id="parametrized-functions">parametrized functions</h2>
<p>Types used in function signature can be parametrized. If we wanted a function that returns a first element of any list, we could have following signature: <code>first :: [a] -&gt; Maybe a</code></p>
<p><code>first</code> takes single parameter, list of <code>a</code> and returns <code>Maybe a</code>. <code>Maybe</code> is a type that is used to signify a value that might or might not be present and has following definition:</p>
<pre><code>data Maybe a =
     Nothing
     | Just a</code></pre>
<p>So our function would return <code>Nothing</code> when given an empty list and <code>Just a</code> when given a list of at least one element.</p>
<h2 id="using-functions">using functions</h2>
<p>Function application in Haskell doesn’t require parentheses around arguments. Calling our <code>add</code> function is just <code>add 1 2</code>. If one of the values is result of another function call, we need to tell which parameters belong to which function. Using <code>$</code> is one option: <code>add 1 $ add 2 3</code>, another option is to use parentheses: <code>add 1 (add 2 3)</code>.</p>
<p>When function is called with less parameters than it expect, instead of run time error you’ll going to receive a function. In following example <code>addLots 5</code> will produce same value as <code>add 1000 5</code>:</p>
<pre><code>addLots = add 1000
addLots 5</code></pre>
<p>Another contrived example of partial application:</p>
<pre><code>findPodcasts :: [Podcast] -&gt; Text -&gt; [Podcast]
search = findPodcasts loadedPodcasts
myPodcasts = search &quot;Tuula&quot;</code></pre>
<h2 id="functions-as-types">functions as types</h2>
<p>Functions have type (that’s what the signature is for after all) and functions can be used as values. You can return function from another function or you can pass in a function as a parameter.</p>
<p>Common example is <code>filter</code>, which has following signature: <code>filter :: (a -&gt; Bool) -&gt; [a] -&gt; [a]</code></p>
<p>It takes two parameters, first one is function that has type <code>a -&gt; Bool</code> and second one is list of <code>a</code>. Return value is list of <code>a</code>. You can produce a list of odd numbers between 1 and 10 with <code>filter odd [1..10]</code>.</p>
<h2 id="anonymous-functions">anonymous functions</h2>
<p>Sometimes you need a function to pass in as a parameter, but the function is so small that you don’t want to give it a name. For those cases, anonymous function are good. If you wanted to produce a list of odd numbers that are greater that 5 in range from 1 10, you could write it as: <code>filter (\x -&gt; odd x &amp;&amp; x &gt; 5) [1..10]</code>. If you squint hard enough <code>\</code> looks almost like a lowercase greek letter λ.</p>
<p>Easiest way to catch me is either email or fediverse where I’m Tuula@mastodon.social</p>
