为什么这些字符串在球拍中不同

Why are these string different in Racket

我 运行 以下代码:

(define  myframe  (new frame% [label "myframe"]))

(define tf1 (new text-field%  [parent myframe] [label "tf1"]))
(define tf2 (new text-field%  [parent myframe][label "tf2"]))
(define tf3 (new text-field%  [parent myframe][label "tf3"]))

(send  myframe show #t)

(define combined_str (string-append (send tf1 get-value) "-" (send tf2 get-value) "-" (send tf3 get-value) )) 
(println combined_str)
(if (eq? "--" combined_str) "same" "different")

输出为:

"--"
"different"

combined_str 是“--”,因为文本字段是空白的。但它与“--”不同。

这几乎可以肯定是由于使用 eq? 而不是 equal? 造成的。参见 Object Identity and Comarpison for more, and also What is the difference between eq?, eqv?, equal?, and = in Scheme?。简而言之,eq? 进行指针比较,这不是您想要的。

示例:

> (eq? "--" (string-append "-" "-"))
#f
> (equal? "--" (string-append "-" "-"))
#t
> (string=? "--" (string-append "-" "-"))
#t