[Haskell-beginners] Convert String to List/Array of Numbers

Brent Yorgey byorgey at seas.upenn.edu
Wed Sep 8 10:07:01 EDT 2010


On Wed, Sep 08, 2010 at 03:31:19PM +0200, Lorenzo Isella wrote:
> Now, my intended pipeline could be
> 
> read file as string--> convert to list of integers-->pass it to
> hmatrix (or try to convert it into a matrix/array).
> Leaving aside the last step, I can easily do something like
> 
> let dat=readFile "mydata.dat"
> 
> 
> in the interactive shell and get a string, but I am having problems

Note, this may be a bit misleading!  The interactive shell does some
special handling of things involving I/O.  The type of readFile
"mydata.dat" is

  readFile "mydata.dat" :: IO String

That is, an *I/O operation which, when performed*, will yield a String.
This is not at all the same thing as having a String!  In order to get
your hands on the String, you will want to do something like this:

  do dat <- readFile "mydata.dat"    -- dat :: String
     let mat = parseMat dat
     ... do other stuff with mat ...

  parseMat :: String -> [[Integer]]
  parseMat = ...

You may want to read

  http://www.haskell.org/haskellwiki/Introduction_to_IO

or, really, any good Haskell tutorial (e.g. LYAH [1] or RWH [2]) will
cover this.

-Brent

[1] http://learnyouahaskell.com/
[2] http://book.realworldhaskell.org/


More information about the Beginners mailing list