We implement a counter like an object in object-oriented programming
(OOP). The type counter for counters is defined as follows:
The three fields of
counter are closure functions that
correspond to methods associated with an object: getting the count of the
counter, increasing the count of the counter by 1 and resetting the count
of the counter to 0. The following defined function
newCounter
is for creating a counter object (represented as a boxed record of
closure functions):
The state of each created counter object is stored in a reference, which
can only be accessed by the three closure functions in the record that
represents the object. This is often referred to as state encapsulation
in OOP.