Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

Can someone explain me why this expression is evaluated as false?

Thanks.

like image 606
Atikae Avatar asked Apr 04 '12 10:04

Atikae


2 Answers

Use = since you have structural equality for comparing two values:

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

Because == is reference equality, it only returns true if you refer to the same memory location:

let a = [[]]
let b = a

# b == a;;
- : bool = true
like image 91
pad Avatar answered Oct 01 '22 10:10

pad


The == operator in OCaml means "physical equality". However, you have two (physically) different lists. Probably, you want "structural equality", which is tested by =.

like image 29
Matthias Avatar answered Oct 01 '22 11:10

Matthias