Created
December 12, 2013 02:14
-
-
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 ...
This file contains 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
let flip_flop () = | |
let counter = ref 0 | |
in | |
fun () -> (if(!counter = 0) then counter := 1 else counter := 0; !counter);; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lesson learnt:
counter (int ref) is private for every generated function. It is pretty neat