#next previous up next next up previous Next: About this document ... Up: hw2sol Previous: hw2sol CS3 Homework Week 2 Solutions 4.2 Given the following procedure: (define (yawn x) (+ 3 (* x 2))) list all the little people that are involved in evaluating (yawn (/ 8 2)) (Give their names, their specialties, their arguments, who hires them, and what they do with their answers.) Suppose Alonzo hires Yanni (a specialist in yawning), to compute the expression. Yanni will need to first commission Diana (a / expert). Diane knows how to divide 8 and 2, giving the result 4 back to Yanni. Now Yanni can substitute 4 into the body of yawn. The body of the expression looks like this: (+ 3 (* 4 2)) Yanni now commissions Andrew (+) and Melissa (*), and waits for their results. Melissa finds out that 4 times 2 is 8, and returns her result to Andrew, who gratefully accepts the information, adds 3 to 8, and realizes the answer is 11. Andrew passes this off to Yanni, and likewise, Yanni passes 11 to Alonzo, who finally knows that (yawn (/ 8 2)) evaluates to 11. 4.3 Here are some procedure definitions. For each one, describe the function in English, show a sample invocation, and show the result of that invocation. (define (f x y) (- y x)) is a two-argument function that subtracts the first argument from the second. > (f 10 7) -3 > (f 7 10) 3 > (f 20 20) 0 (define (identity x) x) is a one-argument function that returns its argument's value unchanged. > (identity 'hello) hello > (identity (* 3 4)) 12 > (identity (se 'one 'ring 'to 'bring 'them 'all)) (one ring to bring them all) (define (three x) 3) is a one-argument function that ignores its first argument, always returning 3. > (three 'foobar) 3 > (three 1.6) 3 > (three (se 'and 'in 'the 'darkness 'bind 'them)) 3 (define (seven) 7) is a zero-argument function that will always return 7 as its value. > (seven) 7 (define (magic n) (- (/ (+ (+ (* 3 n) 13) (- n 1)) 4) 3)) is the math expression \begin{displaymath}\frac{3n + 13 + (n-1)}{4} - 3\end{displaymath} which, with some algebra, reduces to the nicer expression: $n$ . > (magic 42) 42 > (magic 17) 17 4.6 Define a procedure fourth that computes the fourth power of its argument. Do this two ways, first using the multiplication function, and then using square and not (directly) using multiplication. (define (fourth x) (* (* (* x x) x) x)) (define (square x) (* x x)) (define (fourth-without-multiply x) (square (square x))) 4.9 Define a procedure discount that takes two arguments: an item's initial price and a percentage discount. It should return the new price: > (discount 10 5) 9.50 > (discount 29.90 50) 14.95 Here is one possible solution. (define (discount price percent) (* price (- 100 percent) .01)) Students might have also used exact arithmetic: (define (discount-exact price percent) (* price (- 100 percent) 1/100)) in which case their test results will differ slightly. DrScheme on Unix, for example, gives the following results: > (discount-exact 29.90 50) 14.95 > (discount-exact 10 5) 19/2 > (discount-exact 10. 5) 9.5 Islamic Calendar The Islamic calendar has twelve months, alternatively 30 and 29 days as follows: 30 29 30 29 30 29 30 29 30 29 30 29 Fill in the framework below to complete the definition of the islam-day-of-year function. islam-day-of-year takes two arguments, a month and a date in that month, e.g. 9 and 23 to mean the 23rd day of the 9th month, and should return the corresponding day of the year, 259 for the given example. You may assume that the arguments are legal values for a month and a date in that month. (define (islam-day-of-year month date) (if (odd? month) (+ ) ; odd-numbered month (+ ) ) ) ; even-numbered month Test for function on the following pairs of arguments: (1, 1) (3, 1) (1, 30) (11, 30) (2, 1) (12, 1) (2, 29) (12, 2) Hand in a listing of your islam-day-of-year function, as well as a printout of an Interaction window with the results of the tests of your function indicated clearly. An answer: (define (islam-day-of-year month date) (if (even? month) (+ 30 date (* 59 (/ (- month 2) 2))) ; even month (+ date (* 59 (truncate (/ month 2)))) ) ) ; odd month A nice way to structure your answer would be to use an auxiliary function, for instance, (define (59s-before month) (* 59 (truncate (/ (- month 1) 2))) ) (define (islam-day-of-year month date) (if (even? month) (+ 30 date (59s-before (- month 1))) (+ date (59s-before month)) ) ) Here's the results of those tests: > (islam-day-of-year 1 1) 1 > (islam-day-of-year 3 1) 60 > (islam-day-of-year 1 30) 30 > (islam-day-of-year 11 30) 325 > (islam-day-of-year 2 1) 31 > (islam-day-of-year 12 1) 326 > (islam-day-of-year 2 29) 59 > (islam-day-of-year 12 29) 354 _________________________________________________________________ next up previous Next: About this document ... Up: hw2sol Previous: hw2sol Danny Yoo 2001-02-04