;; ===================================================================================== ;; _____ __ __ ____ _ __ __ ____ ___ ;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54 ;; / / / /|_/ / /_/ / / / /_____/ / / / / / ;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 07 ;; \____/_/ /_/_/ \____/ /_/\____/_/ ;; ;; S .d8888. .d88b. db db db d888888b d888888b .d88b. d8b db .d8888. ;; A 88' YP .8P Y8. 88 88 88 `~~88~~' `88' .8P Y8. 888o 88 88' YP ;; M `8bo. 88 88 88 88 88 88 88 88 88 88V8o 88 `8bo. ;; P `Y8b. 88 88 88 88 88 88 88 88 88 88 V8o88 `Y8b. ;; L db 8D `8b d8' 88booo. 88b d88 88 .88. `8b d8' 88 V888 db 8D ;; E `8888Y' `Y88P' Y88888P ~Y8888P' YP Y888888P `Y88P' VP V8P `8888Y' ;; ;; ===================================================================================== (load "asmt-helper.txt") (header "Professor Ellis" "Sample Solutions to Assignment Seven") (problem "1") ;; ================================================================ ;; GEOM-SUM-ACC ;; ================================================================ ;; INPUTS: X, a non-negative integer ;; N, a non-negative integer ;; K, a non-negative integer ;; X-TO-THE-K, a non-negative integer ;; ACC, a non-negative integer ;; OUTPUT: The geometric sum of (X, N), using an accumulator. ;; ================================================================ (define geom-sum-acc (lambda (x n k x-to-the-k acc) (if (> k n) acc (geom-sum-acc x n (+ k 1) (* x-to-the-k x) (+ acc x-to-the-k))))) ;; ================================================================ ;; GEOM-SUM ;; ================================================================ ;; INPUTS: X, a non-negative integer ;; N, a non-negative integer ;; OUTPUT: The geometric sum of (X, N). ;; ================================================================ (define geom-sum (lambda (x n) (geom-sum-acc x n 0 1 0))) (tester '(geom-sum 10 3)) (tester '(geom-sum 2 10)) (tester '(geom-sum 16 4)) (problem "2 (A)") ;; ================================================================ ;; NTH-RIGHTMOST-DIGIT ;; ================================================================ ;; INPUTS: NUM, a non-negative integer ;; N, a non-negative integer ;; OUTPUT: The Nth rightmost digit of NUM, where N=0 refers ;; to the rightmost digit. ;; ================================================================ (define nth-rightmost-digit (lambda (num n) (if (= n 0) (remainder num 10) (nth-rightmost-digit (quotient num 10) (- n 1))))) (tester '(nth-rightmost-digit 92845 0)) (tester '(nth-rightmost-digit 1701 1)) (tester '(nth-rightmost-digit 74656 4)) (problem "2 (B)") ;; ================================================================ ;; NUM-OCCURS-ACC ;; ================================================================ ;; INPUTS: DIGIT, an integer from 0 to 9, inclusive ;; NUM, a non-negative integer ;; ACC, an accumulator ;; OUTPUT: When called with ACC = 0, the output is the number ;; of occurrences of DIGIT in the decimal reprâ€™n of NUM. ;; Example: (num-occurs-acc 3 32123334 0) ==> 4 ;; ================================================================ (define num-occurs-acc (lambda (digit num acc) (if (< num 10) (if (= digit num) (+ 1 acc) acc) (if (= (remainder num 10) digit) (num-occurs-acc digit (quotient num 10) (+ 1 acc)) (num-occurs-acc digit (quotient num 10) acc))))) ;; ================================================================ ;; NUM-OCCURS-WR -- wrapper function for NUM-OCCURS-ACC ;; ================================================================ ;; INPUTS: DIGIT, an integer from 0 to 9, inclusive ;; NUM, a non-negative integer ;; OUTPUT: The number of occurrences of DIGIT in the decimal ;; representation of NUM. ;; ================================================================ (define num-occurs-wr (lambda (digit num) (num-occurs-acc digit num 0))) (tester '(num-occurs-wr 3 12312344444443)) (tester '(num-occurs-wr 0 100)) (tester '(num-occurs-wr 5 1234)) (problem "3") ;; ================================================================ ;; APPROX-ARCTAN-ACC ;; ================================================================ ;; INPUTS: X, a number ;; N, odd positive integer, last term in the expansion ;; FROM, odd positive integer, current term ;; SIGN, -1.0 or 1.0, signum of current term ;; CURR-POWER, number, FROMth power of X ;; ACC, a number, accumulator ;; OUTPUT: Approximation of arctan(X), calculated with a divergent ;; series of the form ;; ;; 3 5 7 9 N ;; X X X X X ;; arctan(X) = 1 - --- + --- - --- + --- - ... +/- --- ;; 3 5 7 9 N ;; ;; ================================================================ (define approx-arctan-acc (lambda (x n from sign curr-power acc) (if (> from n) (+ x acc) (approx-arctan-acc x n (+ 2 from) (* -1 sign) (* curr-power x x) (+ acc (* sign (/ curr-power from))))))) ;; ================================================================ ;; APPROX-ARCTAN ;; ================================================================ ;; INPUTS: X, a number ;; N, odd positive integer, last term in the expansion ;; OUTPUT: Approximation of arctan(X), calculated with a divergent ;; series of the form ;; ;; 3 5 7 9 N ;; X X X X X ;; arctan(X) = X - --- + --- - --- + --- - ... +/- --- ;; 3 5 7 9 N ;; ;; ================================================================ (define approx-arctan (lambda (x n) (approx-arctan-acc x n 3 -1.0 (* x x x) 0))) (tester '(approx-arctan 0.5 9)) (tester '(approx-arctan -0.617 1)) (tester '(let ((i 1)) (newline) (while (< i 27) (printf "(approx-arctan 0.707 ~A) = ~A" i (approx-arctan 0.707 i)) (set! i (+ i 2)) (newline)))) (problem "4") ;; ================================================================ ;; PRINT-CHECKERBOARD-ACC ;; ================================================================ ;; INPUTS: NUM-ROWS, a non-negative integer ;; NUM-COLS, a non-negative integer ;; CURR-ROW, a non-negative integer ;; CURR-COL, a non-negative integer ;; OUTPUT: None. ;; SIDE-EFFECT: Prints a checkerboard NUM-COLS x NUM-ROWS in size. ;; ================================================================ (define print-checkerboard-acc (lambda (num-rows num-cols curr-row curr-col) (cond ((< curr-row num-rows) (cond ((< curr-col num-cols) (printf "~A " (if (even? (+ curr-row curr-col)) "_" "X")) (print-checkerboard-acc num-rows num-cols curr-row (+ 1 curr-col))) (else (newline) (print-checkerboard-acc num-rows num-cols (+ 1 curr-row) 0)))) (else (printf ""))))) ;; ================================================================ ;; PRINT-CHECKERBOARD ;; ================================================================ ;; INPUTS: NUM-ROWS, a non-negative integer ;; NUM-COLS, a non-negative integer ;; OUTPUT: None. ;; SIDE-EFFECT: Prints a checkerboard NUM-COLS x NUM-ROWS in size. ;; ================================================================ (define print-checkerboard (lambda (num-rows num-cols) (print-checkerboard-acc num-rows num-cols 0 0))) (tester '(print-checkerboard 8 8)) (newline) (tester '(print-checkerboard 17 3)) (newline) (tester '(print-checkerboard 4 9))