user1413410
user1413410

Reputation: 1

Haskell - Import text file to list type

I am beginner in haskell. I have a .txt file in the following format:

Company A, 100, 1000

I need to import each row to a list of one type:

type GerCred = [(String, Int, Int)]

How do I do that?

Update

This is what I have tried so far:

type GerCred = [(String,Int,Int)]
type GerCarb = [(String,Int)]

readGerCredList :: File -> IO GerCred
readGerCredList fname = do contents <- readFile fname return(read contents)

Upvotes: 0

Views: 678

Answers (1)

Paul Johnson
Paul Johnson

Reputation: 17786

Break the problem down into bits.

First, figure out how to read the file into a single big string (hint, look for something that returns "IO String")

Then figure out how to take that string and split it into lines (hint: lines).

Then figure out how to take each line and split it into fields (hint: span, stripPrefix)

Then figure out how to convert each field into the type you need (hint: read).

Then figure out how to put it all together (hint: map)

Don't forget that a String is just a [Char].

Paul.

Upvotes: 3

Related Questions