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.
phrase
call after the 1st recursive replace call & a list binding after the 2nd. – Laine