Last active
August 29, 2015 13:58
-
-
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 (==).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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