Re: Looking for better replace-regexp in CLISP
Pascal Bourguignon <[email protected]>
| Newsgroups | gmane.lisp.clisp.general |
|---|---|
| Message-ID | <[email protected]> |
> On 29 Dec 2016, at 17:04, Jean Louis <[email protected]> wrote: > > Hello, > > I have come up with this dirty solution to replace the string, by > using CLISP's builtin regexp: search. > > Is there any other nicer, simpler, solution for the (replace-string > var1 var2 string) below? > > Jean Louis > > (defun replace-string (var1 var2 string) > (let ((split (regexp:regexp-split var1 string))) ; regexp-split is in CLISP implementation > (format nil "~{~a~}" (butlast (flatten (map 'list #'(lambda (x) (cons x var2)) split)))))) > > (defun flatten (obj) > "From Rosetta code: https://rosettacode.org/wiki/Flatten_a_list#Common_Lisp" > (do* ((result (list obj)) > (node result)) > ((null node) (delete nil result)) > (cond ((consp (car node)) > (when (cdar node) (push (cdar node) (cdr node))) > (setf (car node) (caar node))) > (t (setf node (cdr node)))))) > We can simplify it: CL-USER> (defun replace-string (var1 var2 string) (let ((split (regexp:regexp-split var1 string))) (format nil "~{~a~}" (rest (mapcan (lambda (x) (list var2 x)) split))))) REPLACE-STRING But there’s still a fundamental bug: CL-USER> (replace-string "été" "fut" "0été1234été5678") "0fut34fut78" -- __Pascal J. Bourguignon__ ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot _______________________________________________ clisp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/clisp-list