OCaml boolean expression [[]] == [[]]

I have a function that returns [[]] , and I want to test the result as a unit test. But I found that the expression [[]] == [[]] returns false . Here is a simple test code:

 # [[]] == [[]];; - : bool = false 

Can someone explain to me why this expression evaluates to false?

Thanks.

+8
list expression boolean ocaml
source share
2 answers

Use = , since you have structural equality to compare two values:

 # [[]] = [[]];; - : bool = true 

Since == is a reference equality, it only returns true if you are referring to the same place in memory:

 let a = [[]] let b = a # b == a;; - : bool = true 
+13
source share

The == operator in OCaml means "physical equality." However, you have two (physically) different lists. Perhaps you need "structural equality", which is checked for = .

+9
source share

All Articles