[racket] missing solution 20.2.3 ex:filter-contract

From: Daniel Bastos (dbastos at toledo.com)
Date: Wed Sep 3 09:31:25 EDT 2014

A candidate for a solution.

;; Exercise 20.2.3. Use filter1 to develop a function that
;; consumes a list of symbols and extracts all those that
;; are not equal to 'car. Give filter1's corresponding contract.

;; Solution.

(define (filter1 rel-op alon t)
  (cond
    [(empty? alon) empty]
    [else (cond
            [(rel-op (first alon) t)
             (cons (first alon)
                   (filter1 rel-op (rest alon) t))]
            [else
             (filter1 rel-op (rest alon) t)])]))

;; f: (listof symbol) -> (listof symbol)
;; consumes a (listof symbol), producing a (listof symbol) without
;; any occurrence of 'car
(define (f ls-of-sym)
  (local ((define (cmp sym1 sym2)
            (not (symbol=? sym1 sym2))))
    (filter1 cmp ls-of-sym 'car)))

(check-expect (f (list 'a 'b 'car 'x)) (list 'a 'b 'x))
(check-expect (f (list 'car 'car 'x 'car)) (list 'x))

;; The contract of filter1
;; filter1: (symbol symbol -> boolean) (listof symbol) symbol -> (listof symbol)

Posted on the users mailing list.