2013-02-06 21 views
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,我还能用什么来实现呢?

+0

我不确定你的功能真的应该做什么。你想让你的结果保存两个输入列表中相同位置的元素,还是你想要某种交集?如果是后者,那么重复呢? – danlei

+0

我同意丹尼尔 - 这是一个令人困惑的功能。你能举一些函数调用和预期输出的例子吗?另外,如果函数将两个列表连接在一起,为什么需要四个参数? – zck

+0

您应该始终使用正确缩进的Lisp代码。 –

回答

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 

您只能更改词汇可达的变量。

1

不要在Lisp中使用“输出”参数。更好地从函数返回结果。 另外,CL中有一个函数'intersection',它可以做你想做的事情,所以请使用它,除非它是一个练习(然后你可以查看它的实现)。

相关问题