使用 prolog DCG 来查找和查找替换 - 代码审查
我想出了以下代码来替换 Request
中所有出现的 Find
和 Replace
。将答案放入 Result
中。这是使用 DCG,因此它们都是字符代码列表。客户端代码将使用的谓词是substitute
。
findReplace(_, _, [], []) -->
[]. % The end.
findReplace(Find, Replace, Result, ResultRest) -->
Find, % Found Find.
{ append(Replace, Intermediate, Result) }, % Put in Replace in Find's place.
!, % Make sure we don't backtrack & interpret Find as the next case.
findReplace(Find, Replace, Intermediate, ResultRest).
findReplace(Find, Replace, [ C | Intermediate ], ResultRest) -->
[ C ], % Any other character.
findReplace(Find, Replace, Intermediate, ResultRest).
substitute(Find, Replace, Request, Result):-
phrase(findReplace(Find, Replace, Result, []), Request).
这适用于 SWI-Prolog。有人对我如何改进它有任何意见吗?我正在学习如何使用 DCG's &差异列表。例如,我放入了剪切,以便在找到 Find
后,prolog 永远不会回溯 & 。将其解释为 [ C ]
情况下的普通字符。是否需要这样做,或者是否有更具声明性的方式来做到这一点?
另一个问题 - 是否已经有一个谓词可以做与替代品相同的事情,也许是在原子上?
提前致谢。
I came up w/ the following code to replace all occurences of Find
w/ Replace
in Request
& put the answer in Result
. This is using a DCG, so they are all lists of character codes. The predicate that client code would use is substitute
.
findReplace(_, _, [], []) -->
[]. % The end.
findReplace(Find, Replace, Result, ResultRest) -->
Find, % Found Find.
{ append(Replace, Intermediate, Result) }, % Put in Replace in Find's place.
!, % Make sure we don't backtrack & interpret Find as the next case.
findReplace(Find, Replace, Intermediate, ResultRest).
findReplace(Find, Replace, [ C | Intermediate ], ResultRest) -->
[ C ], % Any other character.
findReplace(Find, Replace, Intermediate, ResultRest).
substitute(Find, Replace, Request, Result):-
phrase(findReplace(Find, Replace, Result, []), Request).
This works in SWI-Prolog. Does anyone have any comments on how I could improve it? I'm learning how to use DCG's & difference lists. E.g., I put in the cut so that, after finding Find
, prolog doesn't ever backtrack & interpret that as an ordinary character in the [ C ]
case. Is this needed, or is there a more declarative way of doing so?
Another question - is there a predicate already available to do the same thing that substitute does, maybe on atoms?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑使用半上下文表示法来替换 DCG 中的子序列:
示例:
此外,下划线比 MixedCaseNamesAsYouSee 更可读。
Consider using semicontext notation to replace subsequences in DCGs:
Example:
Also, underscores_are_much_more_readable thanMixedCaseNamesAsYouSee.
关于第二个问题,即使用原子,我编写了这个实用程序 atomic_list_concat< /a>
示例:
About the second question, i.e. working with atoms, I wrote this utility perusing atomic_list_concat
Example: