Search code examples
schemechicken-scheme

string-substitute* not working


I am trying following code:

(require-extension srfi-13)
(require-extension regex)
(print (string-substitute* "This is a test" '(("a test" . "NO TESTING ZONE" ) ) ) )

It works, with following output:

This is NO TESTING ZONE

But following does not work:

(print (string-substitute* "This is a test" '(("a test" . (string-append "NO " "TESTING") ) ) ) )

Following is the error:

Error: (string-substitute) bad argument type - not a string: (string-append "NO " "TESTING")

Even though, following shows that the output is indeed a string:

(print (string? (string-append "NO " "TESTING")))
#t

Where is the problem and how can this be solved?


Solution

  • This has nothing to do with string-substitute*.

    You're quoting the list, so (string-append "NO " "TESTING") is not evaluated:

    > '(("a test" . (string-append "NO " "TESTING")))
    '(("a test" string-append "NO " "TESTING"))
    

    Use quasiquote:

    `(("a test" . ,(string-append "NO " "TESTING")
    

    or don't quote at all:

    (list (cons "a test" (string-append "NO " "TESTING"))