TEL AVIV UNIVERSITY Department of computer science 0368.1105 – Extended Introduction To computer Science Spring semester, 2012 Homework 2 Due Date: April 5th, 2012 *For all questions, you can assume that inputs are valid and correct 1. What is the result of the evaluation of the following code: (define (foo x1 x2 x3) (display “Inside foo…”) (cond (x1 x2) (else x3))) (define x 0) (foo (= x 0) 0 (/ 5 x)) a. In case the interpreter uses applicative order evaluation? Explain your answer. b. In case the interpreter uses normal order evaluation? Explain your answer. 2. Write a procedure (deep-sum-digit x) that gets a positive integer and calculates the sum of digits of the sum of digits up to a number smaller than 10. For example: 4526 -> 4+5+2+6=17 -> 8 > (deep-sum-digit 4526) 8 1342 -> 1+3+4+2=10 -> 1 > (deep-sum-digit 1342) 1 a) Write a recursive version of deep-sum-digit-r. Show a substitution model evaluation for (deep-sum-digit-r 14). b) Write an iterative version of deep-sum-digit-i. Show a substitution model evaluation for (deep-sum-digit-i 14). 3. What is the time complexity order of growth of func below as a function of x: (define (func x) (func2 (* 2 x))) (define (func2 x) (if (< x 1) 0 (+ (factorial x) (func2 (/ x 2))))) (define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1))))) 4. Consider the function defined as follows: pow2(0) = 1 pow2(n) = 2*pow2(n-1), n > 0 a) b) Write a procedure that computes this function recursively: (define (pow2-r n)…) Write a procedure that computes this function iteratively: (define (pow2-i n)…) *Note: Use the algorithm shown above; do not use the primitive function expt. 5. Write a procedure that computes sin(x) using the following approximation (Taylor series): N sin x n 0 1 n (2n 1)! 1 x2 N 1 x3 x5 x ... 3! 5! (2 N 1)! N x 2 n 1 For example: N sin(0.5) n 0 1 n (2n 1)! 1 0.52 N 1 0.53 0.55 0.5 ... 3! 5! (2 N 1)! N 2 n 1 0.5 The procedure should be defined as (define (find-sin x epsilon)…), where x is the degree in radians, and epsilon is a parameter that defines the number of iterations N.N should be such that the result would not increase or decrease in more than an epsilon if the number of iterations was N+1. For example, for (find-sin 0.5 0.0001) N is 3, and the answer is 0.4794.