我正在搜索一些谓词substitute(+StringInput, +SubString, -Substitution, -SubstitutionLetter),根据该谓词,在+SubString中出现的所有子字符串都被+StringInput本身中没有出现的单个符号所替代。结果由-Substition给出,个性化符号存储在-SubstitionLetter中。
例如:
+StringInput = "(2 + b) + c * 2"
+SubString = "2"
-Substitution = "(a + b) + c * a"
-SubstitutionLetter = "a"或
+StringInput= "I went to school on Monday, if I don't get sick."
+Substring = "I"
-Substitution = "a1 went to school on Monday, if a1 don't get sick."
-SubstitutionLetter = "a1"发布于 2022-07-19 22:40:13
注意:参见https://stackoverflow.com/a/73611612/获得更好的代码。
% Show lists of codes as text
:- portray_text(true).
% Show 100 chars before truncating
:- set_portray_text(ellipsis, _, 100).
% Replace if match found
replace(Find, Replace), Replace --> Find, !, replace(Find, Replace).
% Otherwise accept char-by-char
replace(Find, Replace), [C] --> [C], !, replace(Find, Replace).
% Accept success when reached end
replace(_Find, _Replace) --> [].然后,可以使用例如:
?- phrase(replace(`I`, `a1`), `I went to school on Monday, if I don't get sick.`, R).
R = `a1 went to school on Monday, if a1 don't get sick.`.如果同时提供了“查找”和“替换”文本,则此操作有效。
这些是代码列表,即:
?- `ab` = [97,98].
true.https://stackoverflow.com/questions/73042869
复制相似问题