2011-09-11 35 views

回答

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)))))) 

调用示例:

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

谢谢!如果我只想在我想要的时候调用init,该怎么办? – gumbo

+0

这可以帮助简单和简单地解释'labels'。 –