Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code doesn't work properly because <code>(&lt;$&gt;)</code> is for lifting <em>pure</em> actions into an monadic (actually applicative) context, so your <code>myFunc dir n</code> is of type <code>IO [IO (Map.Map [Char] Double)]</code>; an IO action that, when executed, finds the list of files in a directory, and maps each one to another IO action that, when executed, produces the <code>Map</code> you want — without actually executing any of them. That's probably not what you want :)</p> <p>You want to execute a function returning a monadic action over every element of a list, and return a list of the resulting values. That's what <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v%3amapM">mapM</a> does:</p> <pre><code>mapM :: (Monad m) =&gt; (a -&gt; m b) -&gt; [a] -&gt; m [b] </code></pre> <p>So what you really want is:</p> <pre><code>myFunc dir n = allFilesIn dir &gt;&gt;= mapM (perFileFunc n) </code></pre> <p>You use <code>(&gt;&gt;=)</code> because <code>allFilesIn dir</code> is a monadic action itself, and you want to pass it to a function expecting its result type and returning another action (in this case, <code>mapM</code>).</p> <p>Note that <code>mapM</code> is unlike <code>map</code> in that, in <code>IO</code> (not every monad behaves like this, but most do), it will execute every action before returning the list; this means that the result of every action must collectively fit into memory, and you won't be able to process the results incrementally. If you want that, you'll need something other than <code>mapM</code>, such as iteratees.</p>
 

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