Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The tricky thing to understand about Haskell is how to recognize and compose IO actions. Let's look at some type signatures.</p> <pre><code>dir :: FilePath allFilesIn :: FilePath -&gt; IO [FilePath] perFileFunc :: Int -&gt; FilePath -&gt; IO (Map.Map [Char] Double) </code></pre> <p>Now then, you said that for <code>myFunc</code>:</p> <blockquote> <p>I was expecting a list of maps</p> </blockquote> <p>So for that function, you want the type signature</p> <pre><code>myFunc :: Int -&gt; FilePath -&gt; [Map.Map String Double] </code></pre> <p>Of course, the return type can't be <em>just</em> <code>[Map.Map String Double]</code>, because we need to perform some IO in order to evaluate <code>myFunc</code>. So given an <code>Int</code> and a <code>FilePath</code>, we actually want the return type to be an <em>IO action</em> that <em>produces</em> a <code>[Map.Map String Double]</code>:</p> <pre><code>myFunc :: Int -&gt; FilePath -&gt; IO [Map.Map String Double] </code></pre> <p>Now then, let's look at the IO actions we will be composing to create this function.</p> <pre><code>allFilesIn dir :: IO [FilePath] perFileFunc n :: FilePath -&gt; IO (Map.Map String Double) </code></pre> <p><code>perFileFunc</code> isn't actually an IO action, but it <em>is</em> a function that, given a <code>FilePath</code>, produces an IO action. So let's see...if we run the <code>allFilesIn</code> action, then we can work with that list and run <code>perFileFunc n</code> on each of its elements.</p> <pre><code>myFunc dir n = do files &lt;- allFilesIn dir ??? </code></pre> <p>So what goes in the <code>???</code> spot? We have a <code>[FilePath]</code> at our disposal, since we used <code>&lt;-</code> to run the action <code>allFilesIn dir</code>. And we have a function, <code>perFileFunc n :: FilePath -&gt; IO (Map.Map String Double)</code>. And we want the result to have the type <code>IO [Map.Map String Double]</code>.</p> <p>Stop...Hoogle time! Generalizing the components we have (<code>a = FilePath</code>, <code>b = Map.Map String Double</code>), we <a href="http://haskell.org/hoogle/?hoogle=%5Ba%5D+-%3E+%28a+-%3E+IO+b%29+-%3E+IO+%5Bb%5D" rel="nofollow">hoogle for <code>[a] -&gt; (a -&gt; IO b) -&gt; IO [b]</code></a> (pretending we haven't seen ehird's answer yet). Lo and behold, <code>mapM</code> is the magical solution we were looking for! (or <code>forM</code>, which is just <code>flip mapM</code>)</p> <pre><code>myFunc dir n = do files &lt;- allFilesIn dir mapM (perFileFunc n) files </code></pre> <p>If you desugar this do notation, you'll find it reduces to ehird's answer:</p> <pre><code>myFunc dir n = allFilesIn dir &gt;&gt;= (\files -&gt; mapM (perFileFunc n) files) -- eta reduce (\x -&gt; f x) ==&gt; f myFunc dir n = allFilesIn dir &gt;&gt;= mapM (perFileFunc n) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload