soy*_*wod 1 monads parsing haskell
我正在使用ReadP模块编写一个小型解析器。我有这样的表达:
cmdExpr = string "create" <|> string "add" <|> string "another alias" <|> ...
Run Code Online (Sandbox Code Playgroud)
我想抽象出<|>操作,但我不知道如何。类似的东西intercalate:
getExpr cmds = intercalateM (<|>) $ map string cmds
cmdExpr = getExpr ["create", "add", "another alias", ...]
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您可以使用choice:
Prelude Text.ParserCombinators.ReadP> cmds = ["create", "add", "another alias"]
Prelude Text.ParserCombinators.ReadP> :t choice $ map string cmds
choice $ map string cmds :: ReadP String
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
129 次 |
| 最近记录: |