学习使用Parsec库,作业的一部分。Haskell Parsec跳过没有预定义的所有单词
编辑:欢迎使用其他库的建议,重点是解析。
我想要的是从任何句子中提取大写字母和四个指南针方向的所有单词。例如:“比利时完全位于荷兰南部。”应该找到并返回“比利时南荷兰”。
我无法理解的是如何忽略(吃)任何不是指南针方向的输入。 我希望找到沿
'many (not compassDirection >> space)'
但克(1H)oogle是没有帮助我行的东西。
下面的代码明显停留在“多”功能上。
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 =选择$地图字符串[“北”,“南”,“东”,“西”]'。) – huon
做得好诚实,清晰,解决目前为止的问题并提供您现有的代码。一个很好的问题。 +1 – AndrewC