我对RackUnit的check-eq?为相等的字符串返回false感到有点困惑。
这是我正在尝试的代码:
#lang racket
(require rackunit)
(define (get-output proc)
(let ([out (open-output-string)])
(parameterize ([current-output-port out])
(proc)
(get-output-string out))))
(define output (get-output
(λ () (display "hello"))))
(check-eq? output "hello")运行此测试将导致以下错误:
--------------------
. FAILURE
name: check-eq?
location: unsaved-editor:14:0
actual: "hello"
expected: "hello"
--------------------我以为我理解了eq?的意思,但似乎我还是遗漏了一些东西……为什么这是失败的?
我知道get-output-string调用bytes->string/utf8,它返回传递string?约定的内容,所以我假设这在与文本字符串进行比较时应该是有效的。
发布于 2018-11-20 03:35:52
看起来我把eq?和equal?搞混了。
eq?通过内存引用进行比较。
较宽松的是较长的equal?,它大致按值进行比较。
像往常一样,文档的explain this运行得很好。Here关于eq?,equal?和他们奇怪的朋友eqv?的更多细节。
https://stackoverflow.com/questions/53381368
复制相似问题