Learning to use the Parsec library, part of homework.
EDIT: Suggestions to use other libraries are welcome, the point is the parsing.
What I want, is to extract all words with a capital letter, and four compass directions from any sentence. Example: "Belgium totally lies south of Holland." should find and return "Belgium south Holland".
What I can't figure is how to ignore (eat) any input that is -not- a compass direction. I was looking to find something along the lines of
'many (not compassDirection >> space)'
but g(h)oogle isn't helping me.
Following code is obviously stuck on the 'many' function.
readExpr :: String -> String
readExpr input = case parse (parseLine) "" input of
Left err -> "No match: " ++ show err
Right val -> "Found: " ++ showVal val
parseLine :: Parser GraphValue
parseLine = do
x <- parseCountry
space
many ( some (noneOf " ") >> space )
y <- parseCompass
space
many ( some (noneOf " ") >> space )
z <- parseCountry
return $ Direction [x,y,z]
compassDirection :: Parser String
compassDirection = string "north" <|>
string "south" <|>
string "east" <|>
string "west"
parseCountry :: Parser GraphValue
parseCountry = do
c <- upper
x <- many (lower)
return $ Country (c:x)
parseCompass :: Parser GraphValue
parseCompass = do
x <- compassDirection
return $ Compass x
compassDirection = choice $ map string ["north", "south", "east", "west"].) - huon