1

我在下面有一个宏,它沿着整数中的位迭代。我想像这样集成循环的收集功能:

 (loop for x in '(a b c d e)
      for y in '(1 2 3 4 5)
      collect (list x y) )

我应该如何修改下面的宏来完成上述操作?

(defmacro do-bits ((var x) &rest body)
  "Evaluates [body] forms after binding [var] to each set bit in [x]"
  (let ((k (gensym)))
    `(do ((,k ,x (logand ,k (1- ,k))))
         ((= ,k 0))
       (let ((,var (logand ,k (- ,k))))
         ,@body))))
4

1 回答 1

2

这是一个简单的宏with-collector,应该可以解决问题:

(defmacro with-collector ((&optional (collector-name 'collect)) &body body)
  (let ((result (gensym)))
    `(let ((,result (list)))
       (flet ((,collector-name (arg) (push arg ,result)))
         (progn ,@body)
         (when ,result
           (nreverse ,result)))))

collect默认使用名称:

(with-collector ()
  (collect 'a)
  (collect 'b)); => (A B)

但如果您愿意,您可以使用其他名称(例如,用于嵌套或解决符号冲突)

(with-collector (foo)
  (foo 'bar)
  (foo 'baz)); => (BAR BAZ)

要将它与您的宏集成,只需包装do表单:

(defmacro do-bits ((var x) &rest body)
  "Evaluates [body] forms after binding [var] to each set bit in [x]"
  (let ((k (gensym)))
    `(with-collector ()
       (do ((,k ,x (logand ,k (1- ,k))))
           ((= ,k 0))
         (let ((,var (logand ,k (- ,k))))
           ,@body)))))

并将collect在正文中可用:

(do-bits (x 255) (collect x))
; => (1 2 4 8 16 32 64 128)
(do-bits (x 256) (collect x))
; => (256)
于 2012-09-04T15:49:30.037 回答