Re: Substring Substitution

From: Jamie Andrews; real address _at_ bottom of message (me_at_privacy.net)
Date: 12/11/03

  • Next message: Oskar Bartenstein: "Re: Substring Substitution"
    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.)


  • Next message: Oskar Bartenstein: "Re: Substring Substitution"

    Relevant Pages

    • Re: Computability and logic
      ... say v (which is a string of exactly one ... substring replacement) to be predefined. ... substituting a formula for a sentence letter in a formula; ... to come to grips with such technicalities as rigorous definitions for ...
      (sci.logic)
    • Re: Computability and logic
      ... say v (which is a string of exactly one ... substring replacement) to be predefined. ... substituting a formula for a sentence letter in a formula; ... to come to grips with such technicalities as rigorous definitions for ...
      (sci.logic)
    • Re: Computability and logic
      ... substituting a formula for a sentence letter in a formula; ... We substitute a string that may be ... occupies in s (e.g., in the string 'xxyzuvwyzux', the substring 'yzu' ... Still, no presupposition about subsitution. ...
      (sci.logic)
    • Re: [QUIZ] Longest Repeated Substring (#153)
      ... My hack at the substring problem is based on suffix ... in the original string. ... def initialize ...
      (comp.lang.ruby)
    • Favicon type detection - possible?
      ... this is my cobbled together browser detection ... {string: navigator.userAgent, ... subString: "OmniWeb", ... {// for newer Netscapes ...
      (comp.lang.javascript)