Skip to content

Instantly share code, notes, and snippets.

@shehaaz
Created December 12, 2013 02:14
Show Gist options
  • Save shehaaz/7922216 to your computer and use it in GitHub Desktop.
Save shehaaz/7922216 to your computer and use it in GitHub Desktop.
Fun with References: Write a function flip_flop :- unit -> (unit -> int) flip_flop: Generates a function that takes in UNIT and produces INT. Every time the generated function is called with unit it either produces "0" or "1" e.g: let ff = flip_flop();; ff();; >1 ff();; >0 ff();; >1 ...
let flip_flop () =
let counter = ref 0
in
fun () -> (if(!counter = 0) then counter := 1 else counter := 0; !counter);;
@shehaaz
Copy link
Author

shehaaz commented Dec 12, 2013

Lesson learnt:

counter (int ref) is private for every generated function. It is pretty neat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment