首页 > 解决方案 > 单子的 Data.List 插入

问题描述

我正在使用ReadP模块编写一个小型解析器。我有这个表达:

cmdExpr = string "create" <|> string "add" <|> string "another alias" <|> ...

我想抽象出<|>操作,但我不知道如何。像intercalate

getExpr cmds = intercalateM (<|>) $ map string cmds
cmdExpr = getExpr ["create", "add", "another alias", ...]

有任何想法吗?

标签: parsinghaskellmonads

解决方案


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

推荐阅读