0

我正在用 Lisp 编写一个程序,将两个列表中的公共元素放入一个新列表中。这是我的代码。

(defun test (a b)
  (let ((alist nil) (blist nil))
    (progn
      (join a b alist blist)
      (print blist))))

(defun join (a b alist blist)
  (cond
   ((and (null a) (null b))
    (setf blist (cons alist blist)))
   ((equal (car a) (car b))
    (setf alist (cons (list (car a) (car b)) alist)))
   (t (join (cdr a) (cdr b) alist blist))))

但函数的输出始终是nil。然后我在网上查了一些东西,发现当我尝试使用时setf,它不再指向原始列表,而是指向一个新列表。所以如果我不能使用setf,我还能用什么来实现呢?

4

2 回答 2

1

不要在 Lisp 中使用“输出”参数。更好地从函数返回结果。此外,CL 中有一个函数“intersection”可以满足您的需求,因此请使用它,除非它是一个练习(然后您可以查看它的实现)。

于 2013-02-07T08:03:47.830 回答
1
(defun test (a b)
  (let ((alist nil) (blist nil))   ; two variables initialized to NIL
    (progn                         ; this PROGN is not needed
      (join a b alist blist)       ; you call a function, but ignore the
                                   ; return value? Why?
      (print blist))))             ; since blist was never modified, this
                                   ; can only be the initial value, NIL



(defun join (a b alist blist)      ; four new local variables
  (cond
   ((and (null a) (null b))
    (setf blist (cons alist blist)))    ; why do you set the variable BLIST?
                                        ; you never use it later

   ((equal (car a) (car b))
    (setf alist (cons (list (car a) (car b)) alist)))
                                        ; why do you set the variable ALIST?
                                        ; you never use it later

   (t (join (cdr a) (cdr b) alist blist))))
                                        ; the only recursive call of JOIN

您只能更改在词法上可访问的变量。

于 2013-02-06T09:01:25.650 回答