Page 1 of 1

replace string with another

Posted: Wed Oct 10, 2012 7:33 pm
by mautorrejon
I have issue trying to make this work..what I am supposed to do is to change a string for another one given For instance if
(my-replace '(a (b c) d) '(b c) 'x)
-> ( axd ) -->this is what the result should be

My code is the following, please help

Code: Select all


* (defun my-replace( s1 s2 s3)
        (cond ((null s1)        "empty list")
              ((atom s1)        "one element only")
              (( = (num-sublists s1 ) 0)        (substitute s3 s2 s1))
              (t                (substitute s3 s2 (my-replace  (rest s1) s2 s3)))))

Re: replace string with another

Posted: Thu Oct 11, 2012 1:58 pm
by Goheeca
I'm confused with the string thing. Do you want to create a symbol by merging of other ones? Here is a thread about this. But I give a priority to regular strings, can you use them in your problem?
You can use substitute like this:

Code: Select all

(substitute 'x '(b c) '(a (b c) d) :test #'equalp)

Re: replace string with another

Posted: Fri Oct 12, 2012 12:39 am
by JamesF
You seem to be a little confused about the datatypes you're handling here. Those are lists of symbols, with no strings involved (strings are delimited by double-quotes), and you really should read the note in the hyperspec (http://www.lispworks.com/documentation/ ... .htm#quote) about the consequences of destructively modifying quoted lists. The #'list function would be a much better idea in the list that you want modified, unless you know for sure that #'my-replace is creating a new list - to know this, you'll need to look up the definition of #'substitute in the Hyperspec and check whether it's consing or non-consing.

Common Lisp has a heck of a learning curve; unfortunately, there's not much of a short way to climb it. We've all looked for it, trust me :)