Path: utzoo!utgpu!watmath!att!dptg!rutgers!apple!usc!ginosko!uunet!yale!Duchier-Denys From: Duchier-Denys@cs.yale.edu (Denys Duchier) Newsgroups: comp.lang.lisp Subject: Re: substitution in sequences Message-ID: <71160@yale-celray.yale.UUCP> Date: 30 Aug 89 23:38:46 GMT References: <425@ai.cs.utexas.edu> Sender: root@yale.UUCP Reply-To: Duchier-Denys@cs.yale.edu (Denys Duchier) Organization: Computer Science, Yale University, New Haven, CT 06520-2158 Lines: 25 In-reply-to: farquhar@cs.utexas.edu (Adam Farquhar) In article <425@ai.cs.utexas.edu>, farquhar@cs (Adam Farquhar) writes: > Is there an elegant way to perform substitutions in a sequence where > the new item may be of a different length than the old item? E.g. > > (substitute-it "Common lisp, the Language" "CLtL" > "The right function does not seem to be in CLtL.") > => > "The right function does not seem to be in Common Lisp, the Language." > > or (substitute-it "abc" "1" "10101") > => > "abc0abc0abc" How about: (defun substitute-it (new old seq) (let ((i (search old seq))) (if (null i) seq (concatenate (type-of seq) (subseq seq 0 i) new (substitute-it new old (subseq seq (+ i (length old)))))))) --Denys