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

38 lines
1.5 KiB
Plaintext
Raw Normal View History

---
Test
---
| script | lisp interpreter fixture |
| evaluate | (defun adderx (x) (lambda (y) (+ x y))) |
| evaluate | (setf adder20 (adderx 20)) |
| check | evaluate | (funcall adder20 2) | 22 |
| script | lisp interpreter fixture |
| 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)))))))
-!}}}|
| evaluate | (setq my-counter (counter-class)) |
| check | evaluate | (funcall my-counter) | 1 |
| check | evaluate | (funcall my-counter) | 2 |
| check | evaluate | (funcall my-counter) | 3 |
| evaluate | (toggle-counter-direction) |
| check | evaluate | (funcall my-counter) | 2 |
| check | evaluate | (funcall my-counter) | 1 |
| check | evaluate | (funcall my-counter) | 0 |
| evaluate | (toggle-counter-direction) |
| check | evaluate | (funcall my-counter) | 1 |
| check | evaluate | (funcall my-counter) | 2 |
| check | evaluate | (funcall my-counter) | 3 |