2016-08-13 4 views
-1

に異なっている:なぜこれらの文字列は、私は、次のコードを実行しているラケット

(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である「 - 」テキストフィールドが空白であるため。しかし、それは " - "と同じように来ていません。

+0

の可能な重複(http://stackoverflow.com/questions/16299246/what [スキームにおけるEQ?当量?等しい?および=の差は何ですか?] -e-difference-eq-eqv-equal-and-in-scheme)(Racketのこれらの演算子はScheme標準と互換性があるため) – Sylwester

答えて

4

これはほぼ確実にequal?の代わりにeq?を使用することによって発生します。詳細についてはObject Identity and Comarpison、またWhat is the difference between eq?, eqv?, equal?, and = in Scheme?を参照してください。要するに、eq?はあなたが望むものではないポインタ比較を行います。

例:

> (eq? "--" (string-append "-" "-")) 
#f 
> (equal? "--" (string-append "-" "-")) 
#t 
> (string=? "--" (string-append "-" "-")) 
#t 
関連する問題