6
In this question on code review我被告知使用labels
而不是defun
。我在网上看过,但是我找不到任何方式来使用它,并仍然保持我的代码的方式。在Common Lisp中使用标签
如何在我的代码中使用labels
?
In this question on code review我被告知使用labels
而不是defun
。我在网上看过,但是我找不到任何方式来使用它,并仍然保持我的代码的方式。在Common Lisp中使用标签
如何在我的代码中使用labels
?
(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
谢谢!如果我只想在我想要的时候调用init,该怎么办? – gumbo
这可以帮助简单和简单地解释'labels'。 –