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
val cbs: (int -> unit) list ref = ref [] | |
(* | |
x = ref 4 means x has a reference to a value of type int with initial value 4 | |
x := 5 means update the value x refers to with value 5 | |
val y = (!x) means store the value x refers to in y | |
*) | |
fun onKeyEvent f = cbs := f :: (!cbs) | |
fun onEvent i = | |
let fun loop fs = | |
case fs of | |
[] => () | |
|f::fs' => (f i; loop fs') | |
in loop (!cbs) end | |
(* some client code *) | |
val timesPressed = ref 0 | |
val _ = onKeyEvent (fn _ => timesPressed := (!timesPressed) + 1 | |
fun printIfPressed i = | |
onKeyEvent (fn j => | |
if i=j | |
then print ("you pressed " ^ Int.ToString i) | |
else () |
No comments:
Post a Comment