(define last-digit ; last-digit returns the smallest (rightmost) digit of a ; non-negative integer n. (lambda (n) (remainder n 10))) (define first-digits ; first-digits returns a number which consists of all but the ; rightmost digit of n, which must be a non-negative integer. ; If n is less than 10, then first-digits returns 0. (lambda (n) (quotient n 10))) (define digit? ; digit? returns #t if n is a single-digit integer, #f otherwise. ; n must necessarily be a non-negative integer. (lambda (n) (and (<= 0 n) (<= n 9)))) (define splice-together (lambda (n d) ; splice-together returns the integer formed by having n as the leftmost ; digits and d as the rightmost digit. Thus (splice-together 12 4) -> 124 ; n and d must both be non-negative integers, with 0 <= d <= 9 (+ (* 10 n) d))) (define divides? (lambda (n m) (= (remainder m n) 0))) (define make-verifier (lambda (f m) (define sum-f-of-digits (lambda (n d) (if (digit? n) (f d n) (+ (f d (last-digit n)) (sum-f-of-digits (first-digits n) (+ d 1)))))) (lambda (n) (divides? m (sum-f-of-digits n 1))))) (define isbn-checker (make-verifier * 11)) (define upc-checker (make-verifier (lambda (k d) (if (odd? k) d (* 3 d))) 10)) (define bank-checker (make-verifier (lambda (k d) (if (= k 1) (- d) d)) 9))