Problem 9.15.

Write a procedure type-check that takes as arguments a one-argument procedure f and a one-argument predicate procedure pred. Type-check should return a one-argument procedure that first applies pred to its argument; if that result is true, the procedure should return the value computed by applying f to the argument; if pred returns false, the new procedure should also return #f:

> (define safe-sqrt (type-check sqrt number?))
> (safe-sqrt 16)
4
> (safe-sqrt 'sarsaparilla)
#f

While it might not feel like it, this is a solution that almost writes itself from the problem statement.

“Write a procedure type-check that takes as arguments a one-argument procedure f and a one-argument predicate procedure pred.”

(define (type-check f pred)

“Type-check should return a one-argument procedure…”

(define (type-check f pred)
  (lambda (x)

“…that first applies pred to its argument; if that result is true, the procedure should return the value computed by applying f to the argument; if pred returns false, the new procedure should also return #f

(define (type-check f pred)
  (lambda (x)
    (cond ((pred x) (f x))
          (else #f))))