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

Felipe Lessa felipe.lessa at gmail.com
Wed Sep 8 09:40:39 EDT 2010

On Wed, Sep 8, 2010 at 10:31 AM, Lorenzo Isella
<lorenzo.isella at gmail.com> wrote:
> in the interactive shell and get a string, but I am having
> problems in converting this to a list or anything more
> manageable (where every entry is an integer number
> i.e. something which can be summed, subtracted etc...). Ideally
> even a list where every entry is a row (a list in itself) would
> do.

Well, first of all you can split your input into lists using

    lines :: String -> [String]

Then, you can split each line into columns by using

    words :: String -> [String]

Now, on each of these columns you can convert to an integer by

    read :: Read a => String -> a

So in the end, you'll end up with something of type [[Int]].

Does this help you to go into the right direction?

Cheers! =)

PS: Yes, that link's information sort of applies, but you'll be
handling lists of lists (i.e. rows of columns).


More information about the Beginners mailing list