Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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
Lisp函数解释_Lisp_Common Lisp - Fatal编程技术网

Lisp函数解释

Lisp函数解释,lisp,common-lisp,Lisp,Common Lisp,我在LISP中有一个示例,它从列表的每个级别删除给定的数字: (defun remove_aux (e l) (cond ((equal e l) nil) ((atom l) (list l)) (t(list(apply 'append (mapcar #'(lambda (l) (remove_aux e l)) l)))))) (defun remove_el (e l) (car (remove_aux e l))) 因此,如果它是这样运行的:(remove_

我在LISP中有一个示例,它从列表的每个级别删除给定的数字:

(defun remove_aux (e l)
 (cond 
  ((equal e l) nil)
  ((atom l) (list l))
  (t(list(apply 'append (mapcar #'(lambda (l) (remove_aux e l)) l))))))

 (defun remove_el (e l)
  (car (remove_aux e l)))
因此,如果它是这样运行的:
(remove_el 2'(1232)4))=>(1234))

我不完全理解的是这一行是如何工作的:(t(list(apply'append(mapcar#’(lambda(l)(sterge_aux e l))l)))


如果我有一行没有列表并附加
((t(mapcar#’(lambda(l)(remove#aux e l))l))
结果是
((1)NIL(3)((NIL(3)NIL)(4))
如果它有附加但没有列表
((t(apply'append(mapcar#’(lambda(l)(remove#aux e e e e e e l))l)))
,那么结果是
(1 3 3 3 4)
我不明白为什么,因为我在公共Lisp控制台中应用了
(应用'append'((1)NIL(3)((NIL(3)NIL)(4‘‘)'))
,结果是
((13(NIL(3)NIL)(4))
,所以我真的很困惑。有人能一步一步地向我解释这一切是如何运作的吗?

我已经在下面的代码中做了注释,希望能解释一下发生了什么。你可能会感到困惑,因为我在lambda中被重新定义。。。所以t线(在你的例子中)上有2个“l”,但第一个和第二个不一样

(defun remove_aux (e l)
 (cond 
  ((equal e l) nil) ;if e equals l return nil
  ((atom l) (list l)) ;if l is an atom return a list with just l in it
  (t   ; otherwise...
    (list ;create a list
      (apply 'append ; whose contents are created by appending
                     ; together the lists that come out of this mapcar
                     ; (apply the append method)
        (mapcar #'(lambda (l) ( ; iterate over each of the elements in list l
                                ; the one after the lambda not the one 
                                ; being passed to the lambda. 
                                ; (this is a horrible name choice
                                ; lambda(l-item) would be much better)
                                remove_aux e l
                                ; recursively call this method 
                                ; with e (which was passed in at the start)
                                ; and l which isn't the l passed in,
                                ; but is an entry of it (see why naming's 
                                ; so important?)
                                ; this returns a list
                                ; which will get appended by the append 
                                ; with the results of all the other calls 
                                ; to remove_aux for each item in the outer l
                                )
                  ) l)
      )))))

  (defun remove_el (e l)
    (car (remove_aux e l)
  )
)
那么默认情况在函数中的作用是什么呢。。它将自身应用于lst的每个子列表,并将其包装在一个列表中,因此,如果
l
(a y 2 z)
,而
e
2
,那么
mapcar
的结果是
((a)(y)((z))
,这是
的参数应用append
,它将元素再次连接到一个列表中。连接列表时,要删除的元素是一个空列表,在连接过程中会被有效忽略


由于在帮助器中创建的所有附加列表,因此可以将
应用附加
替换为
(mapcan#'(lambda(l)(remove_aux e l))l
。一种更明显的方法是使用
reduce
,而一种更有效的方法是使用
loop

实现您想要实现的过程基本上类似于以下过程:

(defun remove-all (e l)"Removes all occurrences of e from a list l."
  (cond
   ((null l) '())
   ((equal e (car l)) (remove-all e (cdr l)))    
   ((not (atom (car l)))
    (cons (remove-all e (car l))
          (remove-all e (cdr l))))
   (t (cons (car l)
            (remove-all e (cdr l))))))
;note: the e is not neccessarily an atom, the l is not necessarily a list of atoms.
您问题中的过程有不必要的混乱部分,如附加、映射等

如果你在下面重新编译,我将解释算法


干得漂亮。

这看起来很难看。你能把代码格式化吗?
(defun remove-all (e l)"Removes all occurrences of e from a list l."
  (cond
   ((null l) '())
   ((equal e (car l)) (remove-all e (cdr l)))    
   ((not (atom (car l)))
    (cons (remove-all e (car l))
          (remove-all e (cdr l))))
   (t (cons (car l)
            (remove-all e (cdr l))))))
;note: the e is not neccessarily an atom, the l is not necessarily a list of atoms.