INI File Parser in Haskell I'm learning Haskell at the moment, and have just written my first useful module - a parser fo INI files. I used Parsec. I'd like to know what can be improved here - or maybe I did some things completely wrong and there is a better way. Thanks.

module IniFile (iniFileToMap) where

import Text.ParserCombinators.Parsec
import qualified Data.Map as Map
import Char


parseIniFile :: Parser ([(String, [(String, String)])])
parseSection :: Parser (String, [(String, String)])
parseSectionHeader :: Parser String
parseNameValue :: Parser (String, String)
parseComment :: Parser ()
parseNormalValue :: Parser String
parseQuotedValue :: Parser String
parseValue :: Parser String
parseName :: Parser String
parseInsideQuotes :: Parser String
parseCmtOrSpace :: Parser ()
iniFileToMap :: FilePath -&gt; IO (Map.Map String (Map.Map String String))

parseCmtOrSpace = do{
   spaces;
   skipMany parseComment;
}

parseInsideQuotes = many (noneOf "\"")

parseQuotedValue = between (char '"') (char '"') parseInsideQuotes 

parseNormalValue = many1 (satisfy (\c -&gt; isPrint c &amp;&amp; not (isSpace c)))

parseValue = parseQuotedValue &lt;|&gt; parseNormalValue

parseName =  many1 (satisfy (\c -&gt; isAlpha c || isDigit c || c == '_' || c == '.'));

parseComment = do{
   skipMany1 (char ';');
   skipMany (noneOf "\n");
   spaces;
}

parseNameValue = do{
   name &lt;-  parseName;
   between (skipMany (oneOf " \t")) (skipMany (oneOf " \t")) (char '=');
   value &lt;- parseValue;
   return (name, value);
}

parseSectionHeader = do{
   name &lt;- between (char '[') (char ']') parseName;
   return name;
}

parseSection = do{
   name &lt;- between parseCmtOrSpace parseCmtOrSpace parseSectionHeader;
   values &lt;- endBy1 (parseNameValue) (parseCmtOrSpace);
   return (name, values);
}

parseIniFile  = do{
   result &lt;- many1 (parseSection);
   return result;
}

list2Map list = Map.fromList (map (\e -&gt; (fst e, Map.fromList (snd e))) list)

iniFileToMap path = do{
   result &lt;- parseFromFile parseIniFile path;
      case (result) of
         Left err -&gt; error (show err)
         Right xs -&gt; return (list2Map(xs));
}
The first thing I noticed is that you have grouped all the type signatures together, away from the function bodies. This seems very strange to me. It's way more common (and as far as I'm concerned also way more readable) to have a value's type directly before its definition.

The second thing I noticed were all the braces and semicolons. Unless you have a specific reason to use them, I'd strongly recommend making use of Haskell's significant indentation instead. Much less visual noise. I'm especially confused by the semicolon at the end of parseName's definition as parseName doesn't use do-notation and you didn't use a semicolon on any other definition which didn't use do.



You sometimes surround variables with parentheses (e.g. in values &lt;- endBy1 (parseNameValue) (parseCmtOrSpace), but not in the line name &lt;- between parseCmtOrSpace ... directly above it). I can't see any system to when you do this and when you don't, so it just seems random and inconsistent. So I'd recommend to get rid of the needless parentheses.

Speaking of parentheses, you might also want to get rid of some more parentheses by sprinkling a couple of $ signs in your code, to give it a more "haskellish" feel.



parseSectionHeader = do{
   name &lt;- between (char '[') (char ']') parseName;
   return name;
}

parseIniFile  = do{
   result &lt;- many1 (parseSection);
   return result;
}


Whenever you have something of the form:

foo &lt;- bar
return foo


You can simply just write bar. I.e.:

parseSectionHeader = between (char '[') (char ']') parseName

parseIniFile  = many1 parseSection




iniFileToMap path = do{
   result &lt;- parseFromFile parseIniFile path;
      case (result) of
         Left err -&gt; error (show err)
         Right xs -&gt; return (list2Map(xs));
}


Here the indentation is off - case should not be indented further than result &lt;-. It should be:

iniFileToMap path = do
    result &lt;- parseFromFile parseIniFile path
    case result of
        Left err -&gt; error $ show err
        Right xs -&gt; return $ list2Map xs