I am searching some predicate substitute(+StringInput, +SubString, -Substitution, -SubstitutionLetter)
, whereby all the substrings identic with +SubString
occuring in +StringInput
are substituted by a individual symbol not occuring in +StringInput
itself. The result is given by -Substition
and the individualsymbol is stored in -SubstitionLetter
.
e.g.
+StringInput = "(2 + b) + c * 2"
+SubString = "2"
-Substitution = "(a + b) + c * a"
-SubstitutionLetter = "a"
or
+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"
Note: see https://stackoverflow.com/a/73611612/ for better code.
% 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) --> [].
Then, can use e.g.:
?- 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.`.
This works if both the find and replace text are provided.
These are lists of codes, i.e.:
?- `ab` = [97,98].
true.