List 从方案中的列表中删除项会留下空的空列表元素
我有以下代码:List 从方案中的列表中删除项会留下空的空列表元素,list,scheme,cons,List,Scheme,Cons,我有以下代码: (define (atom? x) (and (not (null? x)) (not (pair? x)))) (define delete (lambda (atom l) (cond ((null? l) '()) ((atom? l) (cond ((not(eq? atom l)) l) (else '()) )
(define (atom? x)
(and (not (null? x))
(not (pair? x))))
(define delete
(lambda (atom l)
(cond
((null? l) '())
((atom? l)
(cond
((not(eq? atom l)) l)
(else '())
)
)
(else (cons (delete atom (car l)) (delete atom (cdr l))) )
)
)
)
目标是从此列表中删除某个字符。比如说,
(delete 'a '(a b a) ) ==> (b)
相反,我得到的是:
(delete 'a '(a b a) )==> (() b ())
我对这个计划是全新的。如果找到该值,我尝试不返回任何内容,但这只会使其表现为:
(delete 'a '(a b a) )==> (#<void> b #<void>)
有什么想法吗?非常感谢
(define delete
(lambda (atom l)
(cond
((null? l) '())
((atom? (car l))
(cond
((eq? atom (car l)) (delete atom (cdr l)))
(else (cons (car l) (delete atom (cdr l))))
)
)
(else (cons (delete atom (car l)) (delete atom (cdr l))))
)
)
)
这个解决方案更基本,更容易从基本原理上理解 而且,这个答案是错误的。如果您使用我的解决方案中的示例对其进行测试,您将看到它对更复杂的列表给出了错误的结果。我的解决方案之所以更加详细,有一个原因:这个问题并不容易,给出一个显然更简单但不正确的解决方案对任何人都没有好处。