There is this code in scheme (from the old SICP course of berkeley) which makes a counter function that persists local state variables at two levels, similar to class variab