2011-02-22 72 views
0
(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)。刪除方案列表中的空元素

如何從生成的列表中跳過空元素以獲得最終結果(2 7)?

回答

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))))))