Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在列表中,如何通过将列表作为参数的函数对列表进行修改?_List_Lisp_Common Lisp_Pass By Value - Fatal编程技术网

List 在列表中,如何通过将列表作为参数的函数对列表进行修改?

List 在列表中,如何通过将列表作为参数的函数对列表进行修改?,list,lisp,common-lisp,pass-by-value,List,Lisp,Common Lisp,Pass By Value,我正在用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

我正在用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
,我还可以使用什么来实现这一点

(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
您只能更改词汇上可访问的变量。

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

我不确定您的函数真正应该做什么。您是希望结果包含两个输入列表中处于相同位置的元素,还是希望某种交集?如果是后者,那么副本呢?我同意daniel的观点-这是一个令人困惑的函数。你能给出一些函数调用和预期输出的例子吗?另外,如果函数将两个列表连接在一起,为什么它需要四个参数?您应该始终使用适当的缩进Lisp代码。