I have a function with the following signature:
Config -> IO (Map.Map String String)
that is used to extract a FilePath from the Config, then load that file (contains lines of text), and then parse the lines into keys and values - which is why I need to save them into map, preferably.
So, I have created the following function:
let lns = liftM lines $ readFile $ getSet $ listF c in
foldM (\m n -> liftM3 Map.insert n n m) (return Map.empty) lns
Now, I would hope that Haskell realizes that I want this to be a IO Map of Strings (for simplicity, I dont parse the lines into keys and vals for now, and just put the whole line in there).
However, I get the following errors:
• Couldn't match type ‘Map.Map String’ with ‘IO’
Expected type: IO (Map.Map String String)
Actual type: Map.Map
String (Map.Map String (Map.Map String String))
• In the expression:
foldM (\ m n -> liftM3 Map.insert n n m) (return Map.empty) lns
In the expression:
let lns = liftM lines $ readFile $ getSet $ listF c
in foldM (\ m n -> liftM3 Map.insert n n m) (return Map.empty) lns
In an equation for ‘getEpisodeNames’:
getEpisodeNames c
| listF c == NotSet = return Map.empty
| otherwise
= let lns = liftM lines $ readFile $ getSet $ listF c
in foldM (\ m n -> liftM3 Map.insert n n m) (return Map.empty) lns
• Couldn't match type ‘[Char]’ with ‘Map.Map String String’
Expected type: Map.Map
String (Map.Map String (Map.Map String String))
Actual type: Map.Map String (Map.Map String String)
• In the expression: liftM3 Map.insert n n m
In the first argument of ‘foldM’, namely
‘(\ m n -> liftM3 Map.insert n n m)’
In the expression:
foldM (\ m n -> liftM3 Map.insert n n m) (return Map.empty) lns
• Couldn't match type ‘[]’ with ‘Map.Map String’
Expected type: IO (Map.Map String String)
Actual type: IO [String]
• In the third argument of ‘foldM’, namely ‘lns’
In the expression:
foldM (\ m n -> liftM3 Map.insert n n m) (return Map.empty) lns
In the expression:
let lns = liftM lines $ readFile $ getSet $ listF c
in foldM (\ m n -> liftM3 Map.insert n n m) (return Map.empty) lns
From that, it seems that return Map.empty
tries to wrap the Map in something else than IO monad, which honestly does not make sense to me.
As Haskell never makes mistakes, it is clear to me that I have messed up somewhere, I just can't find, where (though I am pretty sure it is in the second line, as I have checked the lns for the correct type using :t
in ghci). I also tried rewriting the function to >>= notation instead of let ... in ..., but it didn't help. Thanks for any help.