Using Shortcuts in Common Lisp

In this question about viewing code, I was told to use labels instead of defun . I looked on the Internet, but I could not find a way to use it and still keep my code as it is.

How can I use labels in my code?

+6
source share
1 answer
 (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)))))) 

Call example:

 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 
+6
source

Source: https://habr.com/ru/post/897114/


All Articles