Storing the history of objects in a program is a common task. 
Maintaining the history of an object in a program has traditionally required programmers either to write specific code for handling the historical data, or to use a library which supports history logging.

History variables are variables in a programming language which store not only their current value, but also the values they have contained in the past. Some existing languages do provide support for history variables. However these languages typically have many limits and restrictions on use of history variables.
 

[http://www.bod.com/index.php?id=3435&objk_id=148050 "History Variables:
The Semantics, Formal Correctness, and Implementation of History Variables
in an Imperative Programming Language" by Mallon and Takaoka]

Concept also discussed on LtU and Patents.com.

Task
 
Demonstrate History variable support:

 enable history variable support (if needed) 
 define a history variable
 assign three values
 non-destructively display the history
 recall the three values. 

For extra points, if the language of choice does not support history variables,
demonstrate how this might be implemented.




type HistoryVar[T] = object
  hist: seq[T]
 
ffunc initHistoryVar[T](value: T = T.default): HistoryVar[T] =
  ## Initialize a history variable with given value.
  result.hist.add value
 
func set(h: var HistoryVar; value: h.T) =
  ## Set the history variable to given value.
  h.hist.add value
 
func get(h: HistoryVar): h.T =
  ## Return the current value of history variable.
  h.hist[^1]
 
proc showHistory(h: HistoryVar) =
  ## Show the history starting from oldest values.
  for i, value in h.hist:
    echo i, ": ", value
 
func pop(h: var HistoryVar): h.T =
  ## Pop the current value and return it.
  if h.hist.len > 1: h.hist.pop() else: h.hist[0]
 
 
when isMainModule:
 
  var h = initHistoryVar[int]()   # Initialized to 0.
 
  echo "Assigning three values: 1, 2, 3"
  h.set(1)      # 0, 1
  h.set(2)      # 0, 1, 2
  h.set(3)      # 0, 1, 2, 3
 
  echo "History (oldest values first):"
  h.showHistory()
 
  echo "Current value is ", h.get()
 
  echo "Recall the three values:"
  echo h.pop()  # -> 3, last value removed.
  echo h.pop()  # -> 2, last value removed.
  echo h.pop()  # -> 1, last value removed.
 
  echo "History (note that initial value can never be removed):"
  h.showHistory()