2011-09-11 16 views

risposta

6
(defun example() 
    (let ((a 0) 
     (f nil)) 
    (macrolet ((next (state) 
       `(setf f (function ,state)))) 
     (labels ((init() 
       (setf a 0) 
       (next inc)) 
       (inc() 
       (incf a) 
       (next inc) 
       (when (> a 5) 
        (next reset))) 
       (reset() 
       (setf a 0) 
       (next inc)) 
       (controller() 
       (funcall f) 
       (print a))) 
     (init) 
     (loop repeat 20 
       do (controller)))))) 

Esempio chiamata:

CL-USER 7 > (example) 

1 
2 
3 
4 
5 
6 
0 
1 
2 
3 
4 
5 
6 
0 
1 
2 
3 
4 
5 
6 
NIL 
+0

Grazie! e se volessi che init sia chiamato solo quando volevo? – gumbo

+0

Questo potrebbe aiutare con una spiegazione semplice e semplice di 'labels'. –