Here's something based on the code you gave:
import qualified Data.Char as Char -- good practice to import modules qualified
main = do contents <- readFile "input.txt"
print . map (f . words) $ lines contents
where
-- this will break if your input file is badly formed!
f [a,b,c] = (readChar a, readChar b, readFloat c)
readChar :: String -> Char
readChar c = Char.chr (64 + read c)
readFloat :: String -> Float
readFloat = read
The readChar
function reads a string like "1"
as an int, then adds 64 (to bring it into the ascii alphanumeric range) and uses the function Char.chr
to convert it to a character.
You could change the type of readChar
to e.g. readOrd :: (Read a, Ord a) => String -> a
to read something more generic.
Of course, you'll have to do something with those values other than print them (e.g. send them to buildGraph
) or the runtime won't be able to deduce which instance of the intersection Read ∩ Ord
you want.
This will read the file input.txt
, which looks like this:
1 2 4.5
1 3 6.0
3 2 1.2
and output
ghci> main
[('A','B',4.5), ('A','C',6.0), ('C','B',1.2)]