Does != have meaning in OCaml?

前端 未结 5 1693
抹茶落季
抹茶落季 2020-12-07 22:50

It seems to be an equivalency comparison for some types, but not strings.

# 3 != 3;;
- : bool = false
# 3 != 2;;
- : bool = true

This is as

5条回答
  •  太阳男子
    2020-12-07 23:10

    you have experienced the difference between structural and physical equality.

    <> is to = (structural equality) as != is to == (physical equality)

    "odg" = "odg"  (* true  *)
    "odg" == "odg" (* false *)
    

    is false because each is instantiated in different memory locations, doing:

    let v = "odg"
    v == v (* true *)
    v = v  (* true *)
    

    Most of the time you'll want to use = and <>.

    edit about when structural and physical equality are equivalent:

    You can use the what_is_it function and find out all the types that would be equal both structurally and physically. As mentioned in the comments below, and in the linked article, characters, integers, unit, empty list, and some instances of variant types will have this property.

提交回复
热议问题