不要循环,重复! (通用Lisp)

时间:2019-02-08 16:46:57

标签: loops for-loop common-lisp

我在切换到某些循环代码的迭代版本时遇到麻烦:

(defun get-bound-?vars-1 (tree)
  (loop for item in tree
        when (consp item)
          if (member (car item) '(exists forall doall))
            nconc (delete-if-not #'?varp
                    (alexandria:flatten (second item)))
            else nconc (get-bound-?vars item)))

我相应的迭代翻译:

(defun get-bound-?vars-2 (tree)
  (iter (for item in tree)
        (when (consp item)
          (if (member (car item) '(exists forall doall))
            (nconc (delete-if-not #'?varp
                     (alexandria:flatten (second item))))
            (nconc (get-bound-?vars item))))))

作为测试用例:

(defparameter *tree* 
  '(if (exists (?t transmitter)
         (and (connecting ?t ?connector)
              (bind (color ?t $hue))))
     (if (not (exists ((?t1 ?t2) transmitter)
                (and (connecting ?t1 ?connector)
                     (connecting ?t2 ?connector)
                     (bind (color ?t1 $hue1))
                     (bind (color ?t2 $hue2))
                     (not (eql $hue1 $hue2)))))
       (activate-connector! ?connector $hue))))

然后循环确定:

(get-bound-?vars-1 *tree*) => (?T ?T1 ?T2)

但不能迭代:

(get-bound-?vars-2 *tree*) => NIL

感谢任何指针。

0 个答案:

没有答案