I'm a beginner in Haskell, and I'm trying to learn few concenpts so I would appretiate your help. I read few books and guides but I can't understand how can I use IO operations. I'm trying to parse file in this format and create graph object. So far I have this code: (nodes are identified by positive integer)
data Edge = Edge Int Int deriving(Show)
data Graph = Graph {
nodeCount :: Int,
edges :: [Edge]
} deriving (Show)
parseInput :: String -> IO ()
parseInput filePath = do
handle <- openFile filePath ReadMode
contents <- hGetContents handle
putStrLn contents
hClose handle
I understand that haskell is lazy. I want to create function with signature
parseInput :: String -> Maybe Graph
so I either get Nothing
on error or Graph. My problem is that I can't simply read the file and create the graph in one function. As far as I understand, I'm supposed to get IO String, create the object and then close the file using handle? Could anyone point me in the right direction? I'm having a hard time with this because it's very different from imperative languages.
Thanks for any suggestions.