;; (doublerA n): number --> number ;; Return 1 doubled n times. ;; (define (doublerA n) (if (zero? n) 1 (* 2 (doublerA (- n 1))))) (doublerA 0) = 'you-can-replace-this-symbol-with-a-number (doublerA 1) = 'you-can-replace-this-symbol-with-a-number (doublerA 2) = 'you-can-replace-this-symbol-with-a-number (doublerA 3) = 'you-can-replace-this-symbol-with-a-number (doublerA 4) = 'you-can-replace-this-symbol-with-a-number ;; (doublerB n): number --> number ;; Return 1 doubled n times. ;; (define (doublerB n) (if (zero? n) 1 (+ (doublerB (- n 1)) (doublerB (- n 1))))) (doublerB 0) = 'you-can-replace-this-symbol-with-a-number (doublerB 1) = 'you-can-replace-this-symbol-with-a-number (doublerB 2) = 'you-can-replace-this-symbol-with-a-number (doublerB 3) = 'you-can-replace-this-symbol-with-a-number (doublerB 4) = 'you-can-replace-this-symbol-with-a-number ;; (max nums): non-empty-list-of-number --> number ;; Return the largest number in nums. ;; (define (max-of-list nums) (if (empty? (rest nums)) (first nums) (if (> (first nums) (max-of-list (rest nums))) (first nums) (max-of-list (rest nums))))) (max-of-list (cons 4 empty)) = 'replace-this-with-a-number (max-of-list (cons 3 (cons 4 empty))) = 'replace-this-with-a-number (max-of-list (cons 4 (cons 3 empty))) = 'replace-this-with-a-number (max-of-list (cons 2 (cons 3 (cons 4 empty)))) = 'replace-this-with-a-number (max-of-list (cons 4 (cons 3 (cons 2 empty)))) = 'replace-this-with-a-number (max-of-list (cons 1 (cons 2 (cons 3 (cons 4 empty))))) = 'replace-this-with-a-number (max-of-list (cons 4 (cons 3 (cons 2 (cons 1 empty))))) = 'replace-this-with-a-number (max-of-list (cons 1 (cons 4 (cons 2 (cons 3 empty))))) = 'replace-this-with-a-number ;; (threes n) : number --> 1? ;; A strange function: ;; Starting with n, next go to either n/2 (if n even) or 3n+1 (if n odd). ;; Stop when we reach 1. ;; Will we always stop, though? ;; (define (threes n) (if (<= n 1) 1 (if (even? n) (threes (/ n 2)) (threes (+ (* 3 n) 1))))) (threes 0) = 1 (threes 1) = 1 (threes 2) = 1 (threes 3) = 1 (threes 4) = 1 (threes 17) = 1 (threes 567) = 1 (threes 927834789234789423897234879234482793) = 1 ;; If you've written !, see how big (! 400) is. ;; Do you think this "threes" process will whittle away that ;; huge number down to one? ; (threes (! 400)) ;;; Challenge problem: can you write a function ;;; which takes in n, and returns *how many steps* (threes n) required, ;;; to finally reach 1? ;;; Hint: It looks an awful lot like threes itself! ;;; (If you magically knew how many steps the recursive call would take, ;;; could you compute how many steps were taken overall?)