Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Racket hash equality

I've been playing around with Racket and Rackunit. I'm in the process of porting my little static site generator to Racket and writing unit tests and ran into this weird problem.

#lang racket
(require (planet esilkensen/yaml:2:1))
(require rackunit)

(define some-yaml 
  (string->yaml " - name : ding"))

(check-equal? some-yaml '(#hash(("name" . "ding"))) )

Can someone explain to me why the test fails with the following output:

Welcome to DrRacket, version 5.3.3 [3m].
Language: racket; memory limit: 128 MB.
--------------------
FAILURE
name:       check-equal?
location:   (#<path:/home/ding/Documents/racket/blog-generator> 7 0 119 45)
expression: (check-equal? x '(#hash(("name" . "ding"))))
actual:     (#hash(("name" . "ding")))
expected:   (#hash(("name" . "ding")))
like image 428
Illotus Avatar asked Jun 29 '26 03:06

Illotus


2 Answers

It has to do with mutable vs immutable hashes. The following test will pass:

(check-equal? some-yaml (list (make-hash '(("name" . "ding")))))

where make-hash is the mutable hash constructor.

As Eli mentioned, it's confusing that mutable and immutable hashes print the same way, so I've reported a bug.

like image 146
stchang Avatar answered Jun 30 '26 23:06

stchang


'#hash(...) in your source code is read as an immutable hash, but it looks like the library produces a mutable one. (And it's unfortunate that they're both printed the same.)

like image 35
Eli Barzilay Avatar answered Jun 30 '26 23:06

Eli Barzilay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!