Hi,<div><br></div><div>replace <div>let fl = getAllLengths nums</div><div><br></div><div>by</div><div>fl &lt;- getAllLengths nums</div><div><br></div><div>
, since getAllLengths returns a monadic action.</div><div><br></div><div>The author of the following book is much better at explaining why this is so than I am: <a href="http://learnyouahaskell.com/" target="_blank">http://learnonlineyouahaskell.com/</a>. May I suggest you read it cover to cover, it&#39;s really really good. It is probably the best way to learn Haskell at the moment, together with trying out code snippets like you&#39;re doing right now.</div>

<div><br></div><div>Regards,</div><div>Thomas</div></div><div><br></div><div><br></div><br><div class="gmail_quote">On Mon, Oct 11, 2010 at 6:06 PM, Lorenzo Isella <span dir="ltr">&lt;<a href="mailto:lorenzo.isella@gmail.com" target="_blank">lorenzo.isella@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks a lot Daniel, but I am a bit lost (up to not long ago I did not even know the existence of a control monad...and some unstructured reading did not help).<br>


Some online research about mapM and fmap led me here<br>
<a href="http://en.wikibooks.org/wiki/Haskell/Category_theory" target="_blank">http://en.wikibooks.org/wiki/Haskell/Category_theory</a><br>
and I think I am a bit astray at this point ;-)<br>
<br>
Why does my &quot;simple&quot; snippet below raise a number of errors?<div><br>
Cheers<br>
<br>
Lorenzo<br>
<br>
<br>
import Data.Ord<br>
<br>
import Data.List<br>
<br>
main :: IO ()<br>
<br>
main = do<br>
<br></div>
  let nums=[1,2]<br>
<br>
  let fl = getAllLengths nums<br>
<br>
  putStrLn &quot;fl is, &quot;<br>
  print fl<div><br>
<br>
<br>
filename :: Int -&gt; FilePath<br>
filename i = &quot;file&quot; ++ show i ++ &quot;.dat&quot;<br>
<br>
fileLength :: FilePath -&gt; IO Int<br>
fileLength file = fmap length (readFile file)<br>
<br>
getAllLengths :: [Int] -&gt; IO [Int]<br>
getAllLengths nums = mapM (fileLength . filename) nums<br>
<br>
<br>
<br></div><div><div></div><div>
On 10/11/2010 05:21 PM, Daniel Fischer wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Monday 11 October 2010 16:56:58, Lorenzo Isella wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Dear All,<br>
Another I/O question.<br>
Let us say that you are given a list of files file1.dat,<br>
file2.dat...file10.dat and so on (i.e. every file is indexed by a number<br>
and every file is a single column where every entry is a string without<br>
spaces).<br>
In the snippet below I read file1.dat, convert it to a list and then<br>
print out its length.<br>
Now, how can I iterate the process on file1.dat, file2.dat and file3.dat<br>
and store the lengths in a list?<br>
</blockquote>
<br>
fileLength :: FilePath -&gt;  IO Int<br>
fileLength file = fmap length (readFile file)<br>
<br>
filename :: Int -&gt;  FilePath<br>
filename i = &quot;file&quot; ++ show i ++ &quot;.dat&quot;<br>
<br>
getAllLengths :: [Int] -&gt;  IO [Int]<br>
getAllLengths nums = mapM (fileLength . filename) nums<br>
<br>
<br>
If you want something other than the character count, instead of fileLength<br>
use e.g.<br>
<br>
countLines :: FilePath -&gt;  IO Int<br>
countLines file = fmap (length . lines) (readFile file)<br>
<br>
or whatever you&#39;re interested in.<br>
<br>
Another nice thing is often forM (from Control.Monad)<br>
<br>
forM nums $ \i -&gt;  do<br>
    let filename = &quot;file&quot; ++ show i ++ &quot;.dat&quot;<br>
    contents&lt;- readFile filename<br>
    let result = function contents<br>
    doSomethingOrNot<br>
    return result<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I would like to map the file reading and following operations on the<br>
list [1,2.3], but that is giving me a headache.<br>
It is relatively easy to create the file name<br>
<br>
filename=&quot;file&quot;++(show i)++&quot;.dat&quot;   , for i=1,2,3<br>
<br>
but it the the iteration part that is giving me troubles.<br>
Any suggestion is appreciated.<br>
Cheers<br>
<br>
Lorenzo<br>
</blockquote></blockquote>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br>