我有一个问题:如何过滤或映射过滤掉我不想要的列表的函数? 这里是任务: 不允许递归,您必须使用 filter 或 map 来循环遍历列表。 (define-struct Fruit(名称价格))
;[结构列表列表][数字列表]-->[结构列表]
(定义 myfruit 水果不想要的价格)...)
检查期望:输入:
(myfruit
(list (list (make-fruit 'apple 3) (make-fruit 'banane 4) (make-fruit 'avocado 5))
(list (make-fruit 'apple 7)(make-fruit 'avocado 5))
(list (make-fruit 'apple 7)(make-fruit 'avocado 9) (make-fruit 'banana 2)))
(list 5 4))
输出:
(list (make-fruit 'apple 7)(make-fruit 'avocado 9) (make-fruit 'banana 2))
(list (make-fruit 'apple 3) (make-fruit 'banane 4) (make-fruit 'avocado 5)) 不见了,因为 avocado'sprice 是 5,banana's price 是 4。 (list (make -fruit 'apple 7)(make-fruit 'avocado 5)) 不见了,因为 avocado 的价格是 5。 我知道处理结构列表,但现在知道如何处理结构列表列表。结构好复杂。
请举例说明如何处理它。
答案 0 :(得分:0)
(define (has-unwanted-price lof p)
(ormap (λ (f) (= (fruit-price f) p)) lof))
(define (has-unwanted-price-any lof lop)
(ormap (λ (p) (has-unwanted-price lof p)) lop))
(define (myfruit fruits unwanted-prices)
(flat (filter (λ (fruit-list) (not (has-unwanted-price-any fruit-list unwanted-prices))) fruits)))
(define (flat l)
(apply append l))