History variables: Difference between revisions

m
{{out}}
(Adds Clojure solution)
m ({{out}})
Line 1:
{{task}}
''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.''
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]
The Semantics, Formal Correctness, and Implementation of History Variables
in an Imperative Programming Language" by Mallon and Takaoka]
 
Concept also discussed on [http://lambda-the-ultimate.org/node/3111 LtU] and [http://www.patents.com/us-7111283.html Patents.com].
Line 17 ⟶ 20:
* recall the three values.
 
For extra points, if the language of choice does not support history variables, demonstrate how this might be implemented.
demonstrate how this might be implemented.
 
=={{header|Ada}}==
Line 23 ⟶ 27:
Ada does not natively support history variables -- we have to implement them.
 
Furthermore, Ada is a strongly typed language -- that means, we would need to write a history variable type for every basic item type. Instead, we write a single generic package "History_Variables" that works for any item type.
to write a history variable type for every basic item type.
Instead, we write a single generic package "History_Variables" that works for any item type.
 
 
Line 139 ⟶ 145:
end Test_History;</lang>
 
{{out}}
The program generates the following output:
 
<pre> 3 7 9
19</pre>
Line 180 ⟶ 185:
end Test_History;</lang>
 
{{out}}
This time, the output is:
 
<pre>one oneone three
14</pre>
Line 275 ⟶ 279:
}</lang>
 
{{out}}
'''Sample Output'''
 
<pre>foobar <- foo <- 5
</pre>
 
=={{header|Clojure}}==
Clojure does not have history variables, but it can be accomplished via a watcher function that can track changes on a variable.
via a watcher function that can track changes on a variable.
 
<lang clojure>
Line 544 ⟶ 548:
}
}</lang>
{{out}}
Output:
<pre>
History of variable h:
Line 931 ⟶ 935:
 
History::off($x);
print "\$x is: $x\n";</lang>Output<lang>History: a b c d
{{out}}<lang>History: a b c d
undo 1, current value: c
undo 2, current value: b
Line 997 ⟶ 1,002:
 
sys.settrace(trace)
main()</lang>Output<lang>c: 4 -> undo x3 -> 1
{{out}}<lang>c: 4 -> undo x3 -> 1
HIST: {'a': [10, 20], 'i': [0, 1, 2, 3, 4], 'c': [0, 1], 'name': ['c']}</lang>
 
Line 1,076 ⟶ 1,082:
Turn history off <# 定义变量史>__off</#> </lang>
 
{{out}}
Sample output
<pre>Turn history on
Notify Protium we are interested in the variable mv
Line 1,141 ⟶ 1,147:
</lang>
 
{{out}}
'''output'''
<pre style="overflow:scroll">
x history:
Line 1,219 ⟶ 1,225:
end /*?j*/
return ?j-1 /*return the num of assignments. */</lang>
{{out}}
'''output'''
<pre style="overflow:scroll">
fluid= -2.5
Line 1,420 ⟶ 1,426:
puts $foo
histvar foo stop</lang>
{{out}}
Output:
<pre>
quick brown fox
Anonymous user