2 ms·
Common Lisp does not have any built-ins for declaring static local variables known e.g. from C. int test_function() { static int counter = 0; r
by phoe-krk 5y ago
Common Lisp does not have any built-ins for declaring static local variables known e.g. from C.
int test_function() {
static int counter = 0;
return counter++;
}
test_function(); // -> 0
test_function(); // -> 1
test_function(); // -> 2
So, well, we can just implement them ourselves. It's Lisp, after all!
(defun test-function ()
(static-let ((counter 0))
(incf counter)))
(test-function) ; -> 1
(test-function) ; -> 2
(test-function) ; -> 3
This article is done in a literate programming style, attempting both to teach the basics of Lisp writing style and to describe the details of this static binding implementation.