transcendental-lisp/fitnesse/FitNesseRoot/LispInterpreter/StaticVariable.wiki

36 lines
1.2 KiB
Plaintext

---
Test
---
Shows the usage of a static variable.
''"Let Over Lambda Over Let Over Lambda"''
| script | lisp interpreter fixture |
| show | evaluate | !-
(let ((direction 'up))
(defun toggle-counter-direction ()
(setq direction
(if (eq direction 'up)
'down
'up)))
(defun counter-class ()
(let ((counter 0))
(lambda ()
(if (eq direction 'up)
(setq counter (+ counter 1))
(setq counter (- counter 1)))))))
-!|
| show | evaluate | (setq my-counter (counter-class)) |
| check | evaluate | (funcall my-counter) | 1 |
| check | evaluate | (funcall my-counter) | 2 |
| check | evaluate | (funcall my-counter) | 3 |
| show | evaluate | (toggle-counter-direction) |
| check | evaluate | (funcall my-counter) | 2 |
| check | evaluate | (funcall my-counter) | 1 |
| check | evaluate | (funcall my-counter) | 0 |
| show | evaluate | (toggle-counter-direction) |
| check | evaluate | (funcall my-counter) | 1 |
| check | evaluate | (funcall my-counter) | 2 |
| check | evaluate | (funcall my-counter) | 3 |