How to resolve the algorithm History variables step by step in the Common Lisp programming language
How to resolve the algorithm History variables step by step in the Common Lisp programming language
Table of Contents
Problem Statement
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. Demonstrate History variable support: For extra points, if the language of choice does not support history variables, demonstrate how this might be implemented.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm History variables step by step in the Common Lisp programming language
Source code in the common programming language
(defmacro make-hvar (value)
`(list ,value))
(defmacro get-hvar (hvar)
`(car ,hvar))
(defmacro set-hvar (hvar value)
`(push ,value ,hvar))
;; Make sure that setf macro can be used
(defsetf get-hvar set-hvar)
(defmacro undo-hvar (hvar)
`(pop ,hvar))
(let ((v (make-hvar 1)))
(format t "Initial value = ~a~%" (get-hvar v))
(set-hvar v 2)
(setf (get-hvar v) 3) ;; Alternative using setf
(format t "Current value = ~a~%" (get-hvar v))
(undo-hvar v)
(undo-hvar v)
(format t "Restored value = ~a~%" (get-hvar v)))
You may also check:How to resolve the algorithm Sort three variables step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Elena programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Delphi programming language
You may also check:How to resolve the algorithm Mouse position step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Empty program step by step in the AutoIt programming language