Function 从lisp中的整数列表中消除每次出现的x
我需要编写一个lisp函数,从整数列表中消除x的每次出现。例如,(elim7'(76887790))返回(68890)您不需要编写它;它已经为你写了,它叫: 如果确实需要调用它Function 从lisp中的整数列表中消除每次出现的x,function,lisp,common-lisp,Function,Lisp,Common Lisp,我需要编写一个lisp函数,从整数列表中消除x的每次出现。例如,(elim7'(76887790))返回(68890)您不需要编写它;它已经为你写了,它叫: 如果确实需要调用它elim,可以使用(setf fdefinition): 假设您确实需要编写它,通常的方法适用: 递归的 (defun elim (value list) (if list (let ((c (car list))) (if (= c value) (elim value (cdr
elim
,可以使用(setf fdefinition)
:
假设您确实需要编写它,通常的方法适用: 递归的
(defun elim (value list)
(if list
(let ((c (car list)))
(if (= c value)
(elim value (cdr list))
(cons c (elim value (cdr list)))))
(reverse list)))
(defun elim (value list)
(labels ((sub (list res)
(if list
(let ((c (car list)))
(if (= c value)
(sub (cdr list) res)
(sub (cdr list) (cons c res))))
(reverse res))))
(sub list '())))
尾部递归
(defun elim (value list)
(if list
(let ((c (car list)))
(if (= c value)
(elim value (cdr list))
(cons c (elim value (cdr list)))))
(reverse list)))
(defun elim (value list)
(labels ((sub (list res)
(if list
(let ((c (car list)))
(if (= c value)
(sub (cdr list) res)
(sub (cdr list) (cons c res))))
(reverse res))))
(sub list '())))
循环
(defun elim (value list)
(loop for i in list
unless (= i value)
collect i))
向下递归列表,直到列表为空。如果列表的标题等于提供的项目,则不要将其包含在输出中:
(defun elim (value list)
(if (null list)
'()
(let ((next (car list))
(rest (cdr list)))
(if (= value next)
(elim value rest)
(cons next (elim value rest))))))
到目前为止你试过什么?“询问代码的问题必须表明对正在解决的问题的最低理解。包括尝试的解决方案、为什么它们不起作用以及预期的结果。”以及为什么需要写这篇文章?Common Lisp(用于标记问题)已经包含了一个remove
函数。