Re: Substring Substitution
From: Jamie Andrews; real address _at_ bottom of message (me_at_privacy.net)
Date: 12/11/03
- Previous message: xunitinmullik: "Re: can we OR the clauses for satisfying the rule??"
- In reply to: Rob Myers: "Substring Substitution"
- Next in thread: Rob Myers: "Re: Substring Substitution"
- Reply: Rob Myers: "Re: Substring Substitution"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 10 Dec 2003 23:15:02 GMT
Rob Myers <robmyers@mac.com> wrote:
> I would like to replace a substring with a string of a different
> length.
> So something like:
> substitute("Source {tag} string.", "{tag}", "content", X).
> X = "Source content string."
substitute(X, S, T, Y) :-
append(S, Xt, X), % i.e. S is the first part of X, the rest is Xt
!,
substitute(Xt, S, T, Yt),
append(T, Yt, Y).
substitute([Xh|Xt], S, T, [Xh|Yt]) :-
substitute(Xt, S, T, Yt).
Notes:
1.) Works because in Prolog a string is represented as a list of
character codes.
2.) Doesn't do intelligent Boyer-Moore style pattern matching
to find occurrence, thus is probably n^2 in the worst case.
3.) Can't re-substitute after substituting once.
4.) You can make it tail-recursive and therefore more efficient
by switching the order of the last two subgoals in the first
clause. (It'll still work!)
--Jamie. (nel mezzo del cammin di nostra vita)
andrews .uwo } Merge these two lines to obtain my e-mail address.
@csd .ca } (Unsolicited "bulk" e-mail costs everyone.)
- Previous message: xunitinmullik: "Re: can we OR the clauses for satisfying the rule??"
- In reply to: Rob Myers: "Substring Substitution"
- Next in thread: Rob Myers: "Re: Substring Substitution"
- Reply: Rob Myers: "Re: Substring Substitution"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|