<br><div class="gmail_quote"><br>You should use:<div class="im"><br><br>convert = map (map read . words) . lines<br><br></div>Or:<div class="im"><br><br>convert x = (map (map read . words) . lines) x<br><br></div>Or, yet:<div class="im">
<br><br>convert x = map (map read . words) . lines $ x<br><br><br></div>The function application occurs before the dot composition!<div><div></div><div class="h5"><br>
<br><br><br><div class="gmail_quote">On Wed, Sep 8, 2010 at 14:24, 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: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Hi Daniel,<br>
Thanks for your help.<br>
I have a couple of questions left<br>
(1) The first one is quite down to earth.<br>
The snippet below<br>
<br>
---------------------------------------------------<br>
main :: IO ()<br>
<br>
main = do<br>
  txt &lt;- readFile &quot;mydata.dat&quot;<br>
<br>
  let dat = convert txt<br>
<br>
  print dat -- this prints out my chunk of data<br>
<br>
  return ()<br>
<br>
convert x = lines x<br>
<br>
-----------------------------------------------<br>
<br>
pretty much does what it is supposed to do, but if I use this definition of convert x<br>
<br>
convert x = map (map read . words) . lines x<br>
<br>
I bump into compilation errors. Is that the way I am supposed to deal with your function?<br>
<br>
(2) This is a bit more about I/O in general. I start an action with &quot;do&quot; to read some files and I define outside the action some functions which are supposed to operate (within the do action) on the read data.<br>


Is this the way it always has to be? I read something about monads but did not get very far (and hope that they are not badly needed for simple I/O). Is there a way in Haskell to have the action return to the outside world e.g. the value of dat and then work with it elsewhere?<br>


That is what I would do in Python or R, but I think I understood that Haskell&#39;s philosophy is different...<br>
Am I on the right track here? And what is the benefit of this?<br>
<br>
Cheers<br>
<br>
Lorenzo<br>
<br>
<br>
On 09/08/2010 04:06 PM, Daniel Fischer wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
On Wednesday 08 September 2010 15:31:19, Lorenzo Isella wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Dear All,<br>
I must be stuck on something pretty basic (I am struggling badly with<br>
I/O). Let us assume you have a rather simple file mydata.dat (3 columns<br>
of integer numbers), see below.<br>
<br>
<br>
<br>
1246191122 1336 1337<br>
1246191142 1336 1337<br>
1246191162 1336 1337<br>
1246191182 1336 1337<br>
1246191202 1336 1337<br>
1246191222 1336 1337<br>
1246191242 1336 1337<br>
1246191262 1336 1337<br>
1246191282 1336 1337<br>
1246191302 1336 1337<br>
1246191322 1336 1337<br>
1246191342 1336 1337<br>
1246191362 1336 1337<br>
1246191382 1336 1337<br>
1246191402 1336 1337<br>
1246191422 1336 1337<br>
<br>
Now, my intended pipeline could be<br>
<br>
read file as string--&gt;  convert to list of integers--&gt;pass it to hmatrix<br>
(or try to convert it into a matrix/array).<br>
Leaving aside the last step, I can easily do something like<br>
<br>
let dat=readFile &quot;mydata.dat&quot;<br>
<br>
<br>
in the interactive shell and get a string,<br>
</blockquote>
<br>
Not quite. `dat&#39; is the IO-action that reads the file, of type (IO String)<br>
and not a String.<br>
In a programme, you&#39;d do something like<br>
<br>
main = do<br>
     ... -- argument parsing perhaps<br>
     txt&lt;- readFile &quot;mydata.dat&quot;<br>
     let dat = convert txt<br>
     doSomething with dat<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
but I am having problems in<br>
converting this to a list or anything more manageable (where every entry<br>
is an integer number i.e. something which can be summed, subtracted<br>
etc...). Ideally even a list where every entry is a row (a list in<br>
itself) would do.<br>
</blockquote>
<br>
Depending on what the reult type should be, different solutions are<br>
required.<br>
The simplest solutions for such a file format are built from<br>
<br>
read  -- to convert e.g. &quot;135&quot; to 135<br>
lines :: String -&gt;  [String]<br>
words :: String -&gt;  [String]<br>
map :: (a -&gt;  b) -&gt;  [a] -&gt;  [b]<br>
<br>
If you want a flat list of Integers from that file,<br>
<br>
convert = map read . words<br>
<br>
will do. First, `words&#39; splits the String on whitespace (spaces and<br>
newlines), producing a list of digit-strings, those are then read as<br>
Integers.<br>
<br>
If you want a list of lists, each line its own list inside the top level<br>
list,<br>
<br>
convert = map (map read . words) . lines<br>
<br>
is what you want.<br>
<br>
If you want to convert each line into a different data structure, say<br>
(Integer, Double, Int64), the general form would still be<br>
<br>
convert = map parseLine . lines<br>
<br>
and parseLine would depend on the structure you want. For the above,<br>
<br>
parseLine str<br>
     = case words str of<br>
         (a : b : c : _) -&gt;  (read a, read b, read c)<br>
         _ -&gt;  error &quot;Bad line format&quot;<br>
<br>
would be a solution.<br>
<br>
For any but the simplest formats, you should write a real parser to deal<br>
with possible bad formatting though (writing parsers is fun in Haskell).<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
I found online this suggestion<br>
<a href="http://bit.ly/9jv1WG" target="_blank">http://bit.ly/9jv1WG</a><br>
but I am not sure if it really applies to this case.<br>
Many thanks<br>
<br>
Lorenzo<br>
</blockquote>
<br>
</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>
</blockquote></div><br><br clear="all"><br></div></div>-- <br><font color="#888888">Rafael Gustavo da Cunha Pereira Pinto<br><br>
</font></div><br><br clear="all"><br>-- <br>Rafael Gustavo da Cunha Pereira Pinto<br><br>