字符串替换*不工作

string-substitute* not working

我正在尝试以下代码:

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

有效,输出如下:

This is NO TESTING ZONE

但以下不起作用:

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

错误如下:

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

尽管如此,以下显示输出确实是一个字符串:

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

问题出在哪里,如何解决?

这与string-substitute*无关。

您引用的是列表,因此 (string-append "NO " "TESTING") 未被评估:

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

使用准引用:

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

或者根本不引用:

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