(define filter-in
(lambda (predicate list)
(let((f
(lambda (l)
(filter-in-sexpr predicate l))))
(map f list))))
(define filter-in-aux
(lambda (pred lst)
(if (null? lst) '()
(cons (filter-in-sexpr pred (car lst))
(filter-in-aux pred (cdr lst))))))
(define filter-in-sexpr
(lambda (pred sexpr)
(if (equal? (pred sexpr) #t)
sexpr
'())))
调用(过滤号码?'(a 2(1 3)b 7))产生(()2()()7)。
如何从生成的列表中跳过null元素以获得(2 7)的最终结果?
答案 0 :(得分:1)
问题在于您在列表中映射filter-in-sxpr。您可以运行另一个过滤器传递来删除空值,也可以像这样使用修改后的过滤器:
(define filter-in-aux
(lambda (pred lst)
(if (null? lst) '()
(let ((h (filter-in-sexpr pred (car lst)))
(t (filter-in-aux pred (cdr lst))))
(if (null? h) t
(cons h t))))))