(define rightmost-digit ;; returns the last digit of a positive integer (lambda (n) (remainder n 10))) (define left-digits ;; returns the number you get by dropping the last digit of a positive integer (lambda(n) ;; for example, (left-digits 12345) => 1234 (quotient n 10))) (define stick-digit-on-end ;; undoes the previous two (lambda (d n) ;; for example, (stick-digit-on-end 1 4326) => 43261) (+ (* n 10) d))) (define single-digit? (lambda (n) (and (<= 0 n) (< n 10)))) (define number-of-odd-digits (lambda (n) 12)) ;;returns the number of odd digits in n, assumes n>=0 (define num-with-digits-of (lambda (n x) (if (= n 1) x (+ (* x (expt 10 (- n 1))) (num-with-digits-of (- n 1) x))))) (define num-digits-<-6 (lambda (n) (if ( single-digit? n) (if ( < n 6) 1 0) (if (< (rightmost-digit n) 6) (+ 1 (num-digits-<-6 (left-digits n))) (+ 0 (num-digits-<-6 (left-digits n))))))) (define sing-dig (lambda (n) (if (and (< n 10) (odd? n)) 1 0))) (define num-odd-dig (lambda (n) (if (single-digit? n) (sing-dig n) (if (odd? (rightmost-digit n)) (+ (num-odd-dig (left-digits n)) 1) (+ (num-odd-dig (left-digits n)) 0))))) (define sing-dig-6 (lambda (n) (if (< n 6) 1 0))) (define num-dig-less-than-6 (lambda (n) (if (single-digit? n) (sing-dig-6 n) (if (< (rightmost-digit n) 6) (+ (num-dig-less-than-6 (left-digits n)) 1) (+ (num-dig-less-than-6 (left-digits n)) 0))))) (define recur (lambda (a) (if (> a 0) (recur (- a 1)) 3))) (define number-with-digits-of (lambda (n digit) (if (> n 1) (stick-digit-on-end digit (number-with-digits-of (- n 1) digit)) digit)))