开发者

What is wrong with this Haskell? "Parse error on input '='"

I keep getting this error message and I don't understand why:

src\Parsing.hs:21:18: parse error on input `='

It is coming from this line:

tokens  = map strip $ splitWhen (== delimiter) li开发者_运维知识库ne

MY CODE:

tokenize :: String -> HashMap String String
tokenize line = 
   let (delimiter, fieldOrder) = delimiterAndFieldOrderFor line
           tokens  = map strip $ splitWhen (== delimiter) line
   in Map.fromList $  zip fieldOrder tokens

delimiterAndFieldOrderFor :: String -> (Char, [String])
delimiterAndFieldOrderFor line    
       | isInfixOf "," line = (',', ["LastName", "FirstName", "Gender", "FavoriteColor", "BirthDate"])    
       | isInfixOf "|" line = ('|', ["LastName", "FirstName", "Ignored", "Gender", "FavoriteColor", "BirthDate"])    
       | otherwise          = (' ', ["LastName", "FirstName", "Ignored", "Gender", "BirthDate", "FavoriteColor"])

Thanks for your help!


Indentation.

On line 4, tokens should be indented to the same depth as the parens that starts (delim...). E.g.

tokenize line = 
   let (delimiter, fieldOrder) = delimiterAndFieldOrderFor line
       tokens  = map strip $ splitWhen (== delimiter) line
   in Map.fromList $  zip fieldOrder tokens

You might also consider using a where clause:

tokenize line = Map.fromList $ zip f tokens
   where
       (sep, f) = delimiterAndFieldOrderFor line
       tokens   = map strip $ splitWhen (== sep) line

as a matter of style, those very long names for variables are a bit obfuscating, IMO.


You have extra indentation in this code:

let (delimiter, fieldOrder) = delimiterAndFieldOrderFor line
       tokens  = map strip $ splitWhen (== delimiter) line
in Map.fromList $  zip fieldOrder tokens

This causes the compiler to treat the line as a continuation of the expression on the line above it. It should be

let (delimiter, fieldOrder) = delimiterAndFieldOrderFor line
    tokens  = map strip $ splitWhen (== delimiter) line
in Map.fromList $  zip fieldOrder tokens
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜