Question

I am building a graph to implement Dijkstra's algorithm and I am reading a file which contains

1 3 5
1 2 6

How would I read each line and store it as [(a,a,float)]. I need this to be able to use:

buildGraph :: Ord a => [(a, a, Float)] -> Map a [(a, Float)]

Here is How I build my graph:

let g =  buildGraph [('a','c',2), ('a','d',6), ('b','a',3)
                         ,('b','d',8), ('c','d',7), ('c','e',5)
                         ,('d','e',10)]

Currently I can read the file and store everything in an array.

main = do
    contents <- readFile "input.txt"

     print . map readInt . words $ contents


readInt :: String -> Int
readInt = read

I want to be able to reach each file and append to an array that will like this ('1','4',5),which is the same as(a,a,float). after the array will be ready to be sent to buildGraph

Type Edge = (Char, Char, Float)
readGraphFile :: FilePath -> IO Edge
readGraphFile path = do
                    alldata <- readFile path
                    return (Char,Char,Float)
Was it helpful?

Solution

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)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top