Generate a defmacro form that transforms code like this:
(def-iter iterator-product (item-vec indices lengths stopped) (product (&rest iterables) "Cartesian product of input iterables, returned as vectors in lexicographic order. Due to the awkwardness in mixing `&rest` and `&key` parameters in lambda lists, this function does not implement the `repeat` argument supported in [Python's version](https://docs.python.org/3/library/itertools.html#itertools.product). Use `picl:nfold-product` instead. ``` (product '(1 2) '(3 4)) ;; #(1 3), #(1 4), #(2 3), #(2 4) ```" (loop with item-vec = (make-array (length iterables)) with lengths = (make-array (length iterables)) for i below (length item-vec) for iter in iterables do (setf (aref item-vec i) (iter-to-vec iter)) (setf (aref lengths i) (length (aref item-vec i))) minimizing (aref lengths i) into min-len finally (return (if (zerop min-len) (empty-iterator) (init-state item-vec lengths (indices (make-array (length item-vec) :initial-element 0))))))) (if stopped (values nil nil) (multiple-value-prog1 (loop with product = (make-array (length indices)) for i below (length indices) for ii = (aref indices i) do (setf (aref product i) (aref (aref item-vec i) ii)) finally (return (values product t))) (labels ((next-combo (i) (if (< i 0) (setf stopped t) (progn (incf (aref indices i)) (when (>= (aref indices i) (aref lengths i)) (setf (aref indices i) 0) (next-combo (1- i))))))) (next-combo (1- (length indices)))))))

Pattern to recognize: Macro call that should be transformed by def-iter

Macro definition:
(defmacro def-iter (name state-vars (constructor-name constructor-params &body constructor-body)
                    &body next-body)
  "An anaphoric convenience macro which can be re-implemented to change how iterators are
  structured. Exposes the local `init-state` macro within `constructor-body`

  If you don't want to-use this macro (which I totally understand) just write constructors for your
  iterators returning 0-argument closures representing the `next` function and you'll be fine. This
  macro just helped me get through a lot of waffling during development

  Parameters
  ----------
  `name` is a synbol naming the iterator's backing state. However it's actually
  ignored, since PICL doesn't currently expose iterators' backing state

  `state-vars` is a list of symbols naming the iterator's state variables.

  The `constructor-name/params/body` will be `defun`'d to create the user-facing constructor
  function. In `constructor-body`, the return value should be an iterator or a call to local
  `init-state` macro

  `next-body` The definition of the `next` function for this iterator, which will be run in a
   lexical environment consisting of the state vars

  Both `constructor-body` and `next-body` are defined in the lexical environment of the `def-iter`
  form, and as such can access any lexical bindings not shadowed by the `constructor-params` and
  `state-vars` respectively

  The local `init-state` macro.
  ----------------------------
  The arguments to `init-state` should be of the form `(statevar-symbol value-form)`or
  `statevar-symbol`. In the first case, the value of `value-form` will be used to initialize the
  state variable named by `symbol`. In the second case, `statevar-symbol` is also used as the
  corresponding `value-form`

  Example
  -------
  What follows is a sample implementation of the `count` iterator using this macro
  ```
  (def-iter ignored-backing-name (curr-val step)
    (count (start step)
      (init-state (curr-val start) step))
    (prog1 curr-val (incf curr-val step))))
  ````"

  (declare (ignore name))
  (alexandria:with-gensyms (next-fname)
    (let ((docstring "Function had no docstring, so this one was inserted"))
      (when (and (cdr constructor-body) (stringp (car constructor-body)))
        (setf docstring (car constructor-body)
              constructor-body (cdr constructor-body)))
      `(labels ((,next-fname (&key ,@state-vars)
                  (labels ((self () ,@next-body)) #'self)))
         (defun ,constructor-name ,constructor-params
           ,docstring
           (macrolet
               ((init-state (&rest argspecs)
                  (list 'apply '#',next-fname
                        (apply #'append
                               '(list )
                               (mapcar (lambda (aspec)
                                         (if (symbolp aspec)
                                             (list (alx:make-keyword aspec) aspec)
                                             (destructuring-bind (aname adef) aspec
                                               (list (alx:make-keyword aname) adef))))
                                       argspecs)))))
             ,@constructor-body))))))

Macroexpansion:
(labels ((#:next-fname125 (&key item-vec indices lengths stopped) (labels ((self nil (if stopped (values nil nil) (multiple-value-prog1 (loop with product = (make-array (length indices)) for i below (length indices) for ii = (aref indices i) do (setf (aref product i) (aref (aref item-vec i) ii)) finally (return (values product t))) (labels ((next-combo (i) (if (< i 0) (setf stopped t) (progn (incf (aref indices i)) (when (>= (aref indices i) (aref lengths i)) (setf (aref indices i) 0) (next-combo (1- i))))))) (next-combo (1- (length indices)))))))) (function self)))) (defun product (&rest iterables) "Cartesian product of input iterables, returned as vectors in lexicographic order. Due to the awkwardness in mixing `&rest` and `&key` parameters in lambda lists, this function does not implement the `repeat` argument supported in [Python's version](https://docs.python.org/3/library/itertools.html#itertools.product). Use `picl:nfold-product` instead. ``` (product '(1 2) '(3 4)) ;; #(1 3), #(1 4), #(2 3), #(2 4) ```" (macrolet ((init-state (&rest argspecs) (list (quote apply) (quote (function #:next-fname125)) (apply (function append) (quote (list)) (mapcar (lambda (aspec) (if (symbolp aspec) (list (alx:make-keyword aspec) aspec) (destructuring-bind (aname adef) aspec (list (alx:make-keyword aname) adef)))) argspecs))))) (loop with item-vec = (make-array (length iterables)) with lengths = (make-array (length iterables)) for i below (length item-vec) for iter in iterables do (setf (aref item-vec i) (iter-to-vec iter)) (setf (aref lengths i) (length (aref item-vec i))) minimizing (aref lengths i) into min-len finally (return (if (zerop min-len) (empty-iterator) (init-state item-vec lengths (indices (make-array (length item-vec) :initial-element 0)))))))))