仅使用“eq”在常见的lisp中设置相同的功能

时间:2010-12-13 09:19:33

标签: lisp common-lisp

我已经在共同的lisp中编写了一个函数,以比较两个列表以查看它们是否相等而且我已经不习惯使用“相等”谓词我只能使用“eq”而我似乎来了到了墙上。我的代码EVAL出现此错误:变量SETF没有值 可以使用以下重新启动: 他编码:

(defun check(L1 L2)
  (cond
   ((eq L nil) nil)
   (setq x (first L1))
   (setq y (first L2))
   (setf L1 (rest L1))
   (setf L2 (rest L2))
   (if (eq x y) (check L1 L2))))

(defun b(L1 L2)
  (cond
   ((eq L1 nil) nil)
   (setf x (first L1))
   (setf y (first L2))
   (setf L1 (rest L1))
   (setf L2 (rest L2))
   (if (and (list x) (list y)
           (check(x y))
            (if (eq x y) (b(L1 L2))))))

1 个答案:

答案 0 :(得分:0)

我想这就是你要找的东西:

(defun compare-lists (list1 list2)
  (if (and (not (null list1)) 
           (not (null list2)))
      (let ((a (car list1)) (b (car list2)))
        (cond ((and (listp a) (listp b))
               (and (compare-lists a b)
                    (compare-lists (cdr list1) (cdr list2))))
              (t 
               (and (eq a b)
                    (compare-lists (cdr list1) (cdr list2))))))
      (= (length list1) (length list2))))

试验:

? (compare-lists '(1 2 3) '(1 2 3))
T
? (compare-lists '(1 2 3) '(1 2 3 4))
NIL
? (compare-lists '(1 2 3) '(1 2 (3)))
NIL
? (compare-lists '(1 2 (3)) '(1 2 (3)))
T
? (compare-lists '(1 2 (a b c r)) '(1 2 (a b c (r))))
NIL
? (compare-lists '(1 2 (a b c (r))) '(1 2 (a b c (r))))
T