;; Returns a number whose digits are the reverse of the digits of n ;; For example, ;; (reverse 207) returns 702 ;; (reverse 1239) returns 9321 (define reverse (lambda (n) (define helper (lambda (n r) (if (= n 0) r (helper (quotient n 10) (+ (* r 10) (remainder n 10)))))) (helper n 0))) ;; Returns a number whose digits are the digits of n at even positions, ;; in order. For example ;; (even-digits-number-of 207) returns 27 ;; (even-digits-number-of 51239) returns 529 (define even-digits-number-of (lambda (n) (if (= n 0) 0 (+ (* 10 (even-digits-number-of (quotient n 100))) (remainder n 10))))) ;; Returns a number whose digits are the digits of n at odd positions, ;; in order. For example ;; (odd-digits-number-of 207) returns 0 ;; (odd-digits-number-of 51239) returns 13 (define odd-digits-number-of (lambda (n) (if (= n 0) 0 (+ (* 10 (odd-digits-number-of (quotient n 100))) (quotient (remainder n 100) 10))))) ;; Returns a number whose digits are constructed as follows: ;; its digits at odd positions come from odd-digits-number, in order, and ;; its digits at even positions come from even-digits-number, in order. ;; For example, ;; (combine 12 4) returns 1024 ;; (combine 12 34) returns 1324 ;; (combine 123 987654) returns 90807162534 (define combine (lambda (odd-digits-number even-digits-number) (if (and (= odd-digits-number 0) (= even-digits-number 0)) 0 (+ (* 100 (combine (quotient odd-digits-number 10) (quotient even-digits-number 10))) (* 10 (remainder odd-digits-number 10)) (remainder even-digits-number 10)))))