newLISP Fan Club

Forum => newLISP newS => Topic started by: cormullion on January 10, 2006, 06:34:43 AM

Title: Non-destructive replace?
Post by: cormullion on January 10, 2006, 06:34:43 AM
How do I a non-destructive replace? I've realised that my problem is:



(set 'new-filename (replace "." old-file-name "copy."))



- I've destroyed the old-file-name...



It seems wrong to make a safe copy beforehand.
Title:
Post by: Lutz on January 10, 2006, 06:49:48 AM
Just wrap the old filename with anything which returns the filename:



> (set 'old "myfile.txt")
"myfile.txt"
> (replace "." (string old) ".copy.")
"myfile.copy.txt"
> old
"myfile.txt"
>


even 'if', 'or', 'and' or 'begin' would work, but 'string' is more descriptive. Or using implicit 'slice'



(replace "." (0 old) ".copy.")




Lutz
Title:
Post by: cormullion on January 10, 2006, 07:58:25 AM
Quote(replace "." (string old) ".copy.")


That's clever - thanks!
Title:
Post by: Fanda on January 10, 2006, 11:25:38 AM
Instead of a (string old) you could also use:


(set 'old)  or  (setq old)


Fanda