Exercises for Friday, October 29, 2004 Calling and Creating Functions We're going to experiment with the DrScheme Stepper today. The Stepper only works in the Student Languages Beginner through Intermediate; so go to the Languages menu and set the Language to 'Intermediate Student'. Now type the following lines to the upper pane of a DrScheme window. (define (square num) (* num num)) (define (area radius) (* pi (square radius))) (area 7) The constant 'pi' is already defined in Intermediate Student. Now save the file as 'circle.scm' and click on the 'Step' button. If you repeatedly click on 'Step >' button in the stepper pane, you'll see the steps that Scheme takes in evaluating the expression '(area 7)'. In the stepper you'll see how Scheme replaces the invocation of a function, e.g., (area 7), with the definition of the function, e.g., (* pi (square radius)), using the calling pattern, (area radius), specified in the definition. The values of the function arguments are then substituted for the formal parameters in the function definition. 1. Write a function that computes the average of four test scores. (define (average score_1 score_2 score_3 score_4) ... ) > (average 90 86 97 75) 87 2. Write a function 'distance' that computes the Euclidean distance between two points in the plane. If you recall from high school geometry, the distance between two points in the plane is the square root of the sum of the squared differences of the x and y components, i.e., for points p1 = (x1 y1) and p2 = (x2 y2) the distance from p1 to p2 is sqrt of ( (x1 - x2)^2 + (y1 - y2)^2 ). Now translate this expression into Scheme: (define (distance x1 y1 x2 y2) ... ) > (distance 0 1 1 0) 1.4142135623730951 Use the 'square' function above and the built-in 'sqrt' function. Then use the stepper to step through the execution of an example invocation of the 'distance'. 3. In this exercise, you'll define and use a 'point' data structure: (define (make-point x y) (list x y)) (define (x-coord pt) (car pt)) (define (y-coord pt) (car (cdr pt))) Rewrite the 'distance' function to work with the 'point' data structure: (define (distance pt1 pt2) ... ) Create a couple of points: (define point_1 (make-point 1 0)) (define point_2 (make-point 0 1)) And try out your function (distance pt1 pt2) with the stepper. > (distance point_1 point_2) 1.4142135623730951 Now type in the factorial function and save it in 'factorial.scm': (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) (factorial 3) Again watch what happens in the stepper. Now experiment with the last function. Last only works if it is given a list consisting of at least one element. So we first check to see if 'last' is given appropriate input. (define (last arg) (if (and (list? arg) (not (null? arg)) (not (null? (rest arg)))) (aux-last arg) (printf "last: inappropriate argument ~a~%" arg))) We then define an auxiliary function that does all the real work. (define (aux-last lst) (if (null? (rest lst)) (first lst) (aux-last (rest lst)))) > (last '(1 2 3)) 3 4. Write a function 'length' that counts the number of elements in a list. Use two functions 'length' and 'aux-length' as we did for 'last. Your 'aux-length should take a second argument that keeps track of how many elements have been encountered so far. > (length '(1 2 3 4 5)) 5