;; ===================================================================================== ;; _____ __ __ ____ _ __ __ ____ ___ ;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54 ;; / / / /|_/ / /_/ / / / /_____/ / / / / / ;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Quiz 03 ;; \____/_/ /_/_/ \____/ /_/\____/_/ Friday, 22nd February, 2019 ;; ;; .d8888. .d88b. db db db d888888b d888888b .d88b. d8b db .d8888. ;; 88' YP .8P Y8. 88 88 88 `~~88~~' `88' .8P Y8. 888o 88 88' YP ;; `8bo. 88 88 88 88 88 88 88 88 88 88V8o 88 `8bo. ;; `Y8b. 88 88 88 88 88 88 88 88 88 88 V8o88 `Y8b. ;; db 8D `8b d8' 88booo. 88b d88 88 .88. `8b d8' 88 V888 db 8D ;; `8888Y' `Y88P' Y88888P ~Y8888P' YP Y888888P `Y88P' VP V8P `8888Y' ;; ;; ===================================================================================== *** QUESTION 3 *** [ 3 points ] There are several reasons for using contracts: 1. It informs the user of the number of inputs expected by the function. 2. It informs the user of the type of inputs expected by the function. 3. It describes the purpose of the function (i.e. what it does) and usually provides some outline of how it is performed. 4. Any side-effects of the function are described. 5. The output of the function and its data type are given. 6. It helps us remember what we, the programmer, are writing. A contract is an agreement, or a promise: by writing the contract we are better able to remember why we began to write this function. 7. It helps us in debugging (fixing errors) in our programs. This list is not exhaustive, but it does cover the majority of the main points. *** QUESTION 2 *** [ 2 points ] Well-written recursive functions have two defining characteristics: 1. One or more terminating conditions, or 'base cases', at which the function will stop and simply output some value. 2. The function must call itself as part of its own body. *** QUESTION 3 *** [ 2 points ] There are several possible answers. 1. IF can result in only one TRUE/FALSE outcome, where COND can test multiple conditions. 2. For either TRUE or FALSE, IF can have only one expression (i.e. set of parentheses) for that case; COND can have multiple expressions in the same block. 3. COND *must* have an ELSE as the test for the last block. This ensures that if we miss a case, or make an error in our tests, there is some 'fall-through' trap which will provide a specific output or display an error with side-effect printing. *** QUESTION 4 *** [ 5 points ] The SUMTIMES function takes two inputs, NUM and X, and functions according to the rules below. If X <= 0, output 0. If X > 0, output NUM + SUMTIMES(NUM, X - 1). (define sumtimes (lambda (num x) (if (<= x 0) 0 (+ num (sumtimes num (- x 1)))))) [ 12 points ]