# How to compare \`hash\` correctly

**URL:** <https://racket.discourse.group/t/how-to-compare-hash-correctly/412>\
**Category:** Questions & Answers\
**Created:** [December 12, 2021, 8:36am UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412 "2021-12-12T08:36:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [December 12, 2021, 8:36am UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412/1 "2021-12-12T08:36:55Z")

</div>

I'm trying to generate yaml from Racket, once it has been done, I want to check it has the same value as the original reference yaml.

```scheme
(require yaml)

(define origin (file->yaml "origin.yml"))
(define new ...)
(equal? new origin)

```

Currently `(equal? new origin)` produces `#f`, but after I trace into its value, they look the same.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [December 12, 2021, 12:59pm UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412/2 "2021-12-12T12:59:14Z")

</div>

If "they look the same" means that they are the same textual - then convert the values to strings and compare the strings. An easy way to convert a string is to display the value to a string port. Depending on context you could even use `~a` to do the conversion.

---

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [December 12, 2021, 1:32pm UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412/3 "2021-12-12T13:32:32Z")

</div>

They look like same in yaml, but yaml has different formats for one type. So I think this is not an option

Jens Axel Søgaard via Racket Discussions \<[racket@discoursemail.com](mailto:racket@discoursemail.com)\>於 2021年12月12日 週日，下午9:09寫道：

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [December 12, 2021, 2:12pm UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412/4 "2021-12-12T14:12:21Z")

</div>

Do you have a minimal example that shows the problem?

---

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [December 12, 2021, 2:21pm UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412/5 "2021-12-12T14:21:59Z")

</div>

Emmm...... I start to think my code has some problem now, I tried the following code

```scheme
(equal? (make-hash (list (cons "a" (make-hash (list)))))
        (make-hash (list (cons "a" (make-hash (list)))))

```

and it works. So my original question has a problem, and I actually can dump hash into YAML and using `diff`, but still thanks for help!

---

<div class="post-metadata">

**Author:** ![ryanc](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/ryanc/32/71_2.png) [@ryanc](https://racket.discourse.group/u/ryanc)\
**Post date:** [December 12, 2021, 2:33pm UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412/6 "2021-12-12T14:33:36Z")

</div>

Beware, mutable and immutable hashes are not equal even if they have equal contents. For example:

```scheme
> (equal? (make-hash null) (hash))
#f

```

That's a likely reason for hashes not being equal even if they print the same.

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [December 12, 2021, 9:55pm UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412/7 "2021-12-12T21:55:39Z")

</div>

If you only need to generate YAML, not parse it, I've taken advantage of the fact that YAML is defined as a superset of JSON and just used Racket's `json` library to write it. There are some YAML features that can't be expressed as JSON, but I've never needed them in practice, and I think many YAML-using programs don't even support them.

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [December 14, 2021, 10:46pm UTC](https://racket.discourse.group/t/how-to-compare-hash-correctly/412/8 "2021-12-14T22:46:06Z")

</div>

Agreed; I've been convinced that general YAML has some very poor design choices; sticking to a JSON subset seems like a very good idea.
