replace string with another

Discussion of Common Lisp

replace string with another

Postby mautorrejon » Wed Oct 10, 2012 7:33 pm

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)))))
mautorrejon
 
Posts: 2
Joined: Wed Oct 10, 2012 7:28 pm

Re: replace string with another

Postby Goheeca » Thu Oct 11, 2012 1:58 pm

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)
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 165
Joined: Thu May 10, 2012 12:54 pm

Re: replace string with another

Postby JamesF » Fri Oct 12, 2012 12:39 am

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 :)
JamesF
 
Posts: 95
Joined: Thu Jul 10, 2008 7:14 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 1 guest

cron