我有以下代码:
(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>)
有什么想法吗?非常感谢!
答案 0 :(得分:0)
(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))))
)
)
)
从基础知识来看,这个解决方案更基本,更容易理解。