parsing - 单子的 Data.List 插入
问题描述
我正在使用ReadP模块编写一个小型解析器。我有这个表达:
cmdExpr = string "create" <|> string "add" <|> string "another alias" <|> ...
我想抽象出<|>
操作,但我不知道如何。像intercalate
:
getExpr cmds = intercalateM (<|>) $ map string cmds
cmdExpr = getExpr ["create", "add", "another alias", ...]
有任何想法吗?
解决方案
您可以使用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
推荐阅读
- python - python数组没有被填充
- python - 如何在 Python 中的朴素贝叶斯分类模型中进行 for 循环?
- java - Java 在子字符串重复一定次数后替换子字符串
- python - 删除数据框中所有数据点中的多余字符
- javascript - 作为 [object Object] 进入服务器的对象数组
- ios - SQLite:从 API 存储数据时应该使用整数主键还是整数?
- javascript - 新手 REST 服务器设置
- continuous-integration - 使用特定于应用程序的数据将数据库部署到不同的环境中
- python - 如何在类实例化中自动传递重复的 kwargs
- python-3.x - 使用 inset_axes 和 transData 时的宽度和高度错误