Data.List 插入 monads

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)

有任何想法吗?

Mar*_*ann 6

您可以使用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)