(defun element-generator ()
The function is defined above.
(let ((state '(() . (list of elements to be generated)))) ;() sentinel.
A local variable was defined here. The code has literal data. Think about the functions of the Lisp function itself, and part of the code is a list construct. In the Lisp undefined standard, what happens is when you try to modify this data object.
Since this is a data object with letters, there is only one of them. The variable does not get bound to freshly bound data. Thus, with every call here the variable points to the same literals.
(let ((ans (cadr state))) ;pick off the first element
Above creates a temporary variable and saves the first element.
(rplacd state (cddr state)) ;smash the cons
In the above code, the first item is removed from the list. As already mentioned, this is not recommended since the list is literal data.
ans)))
Above returns the stored value.
This LET idiom found in Common Lisp is already provided by the PROG1 macro.
Rainer joswig
source share