;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname filterr) (read-case-sensitive #t) (teachpacks ((lib "docs.rkt" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "docs.rkt" "teachpack" "htdp")) #f))) ; how to use a predicate to filter out certain elements of a list ; while retaining those that pass the predicate (define (filterr pred ys) (cond ((null? ys) '()) ((pred (car ys)) (cons (car ys) (filterr pred (cdr ys)))) (else (filterr pred (cdr ys))))) (filterr (lambda (x) (> x 0)) '(-1 2 -3 4 5)) (filterr number? '(1 ruby ben 3 5 clara)) (filterr atom? '(1 ruby (3) (ben) 4 clara))