exception Empty; fun newStack (x) = let val store = ref [x] in {push = fn y => store := y::(!store), pop = fn () => case !store of nil => raise Empty | (y::ys) => (store := ys; y) } end; val s = newStack(0); #push(s)(1); #pop(s)(); #pop(s)(); fun newCountingStack (x) = let val store = ref [x] val counter = ref 1 in {push = fn y => (counter := !counter + 1; store := y::(!store)), pop = fn () => case !store of nil => raise Empty | (y::ys) => (counter := !counter - 1; store := ys; y), counter = fn () => !counter } end; val cs = newCountingStack(0); #push(cs)(1); #counter(cs)(); #pop(cs)(); #pop(cs)(); #counter(cs)(); (* val l : {pop:unit -> int, push:int -> unit} = [s,cs] *)