Skip to content

Instantly share code, notes, and snippets.

@hkoba
Last active August 29, 2015 13:58
Show Gist options
  • Save hkoba/10021220 to your computer and use it in GitHub Desktop.
Save hkoba/10021220 to your computer and use it in GitHub Desktop.
OCaml tip - To test equality of self referencing data, we can't use (=) since it causes Out_of_memory. In this specific case, we should use (==).
type 'a ring_t = {
value: 'a;
mutable next: 'a ring_t;
mutable prev: 'a ring_t;
}
let init value =
let rec x = {value; next = x; prev = x} in
x
let () =
let x = init 3
and y = init 8
and yesno yn = (if yn then "yes" else "no")
in
(
Printf.printf "x == y ? --> %s\n" (yesno (x == y));
(* --> no *)
Printf.printf "x == x ? --> %s\n" (yesno (x == x));
(* --> yes *)
Printf.printf "x = y ? --> %s\n" (yesno (x = y));
(* --> no *)
Printf.printf "x = x ? --> %s\n" (yesno (x = x))
(* Fatal error: exception Out_of_memory *)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment