Вы находитесь на странице: 1из 2

CSE1010 - Introduction to Programming

September 4, 2013
Problem set 1 solutions

1. Re-write the following arithmetic expressions as SCHEME expressions and show the result of the SCHEME
interpreter when invoked on your expressions.
(a) (22 + 42) (54 99).

(* (+ 22 42)
(* 54 99))
(b) ((22 + 42) 54) 99.

(* (* (+ 22 42)
54)
99)
(c) 64 102 + 16 (44/22).

(+ (* 64 102)
(* 16
(/ 44 22)))
2. Reflect on the expressions above.
(a) Of course, the first two expressions evaluate to the same number. In what sense are they different?
How is this reflected in the SCHEME expression?
The order of operations is different. Observe how this changes the parenthetical structure of the
SCHEME expression.
(b) In an unparenthesized infix arithmetic expression, like 3 + 4 5, we rely on a convention to determine
which operation we apply first (rules of precedence). Are rules of precedence necessary for arithmetic
operations SCHEME?
No. Any such arithmetic expression determines unambiguously which operator to apply first; the same
can be said of all subexpressions!
3. Write SCHEME definitions for the functions below. Use the interpreter to try them out on a couple of test
cases to check that they work, and include this output with your solutions.
(a) inc, the function inc(x) = x + 1.

(define (inc x) (+ x 1))


(b) inc2, the function inc2(x) = x + 2. Show how to write inc2 using your definition of inc instead of
+.

(define (inc2 x) (+ x 2))


Alternatively,

(define (inc2 x) (inc (inc x)))


(c) fourth, the function fourth(x) = x 4 .

(define (fourth x) (* x x x x))

(d) p, the polynomial function p(x) = (x 5 + 16x 4 + 22x 3 + x + 9)2 .


We begin with the polynomial (x 5 + 16x 4 + 22x 3 + x + 9). One trick to express this simply in terms of
primitive arithmetic operations is to observe that
(x 5 + 16x 4 + 22x 3 + x + 9) = 9 + x(1 + x 2 (22 + x(16 + x)))
Then we can express this polynomial as a SCHEME function as follows:

(define (q x)
(+ 9 (* x
(+ 1 (* x x
(+ 22 (* x
(+ 16 x))))))))
The function we want, p, is the square of q:

(define (p x) (* (q x) (q x)))
(e) Using the function fourth, write the function sixteenth(x) = x 16 .

(define (sixteenth x)
(fourth (fourth x)))
(f) Draw the SCHEME environment at the beginning of the evaluation of the fourth calls in the evaluation
of (sixteenth 4). Note that two distinct evaluations of fourth occur here. You do not need to
show inc,inc2,p.

(g) Using the function sixteenth, write the function two-hundred-fifty-sixth(x) = x 256 . Recall
that 256 = 16 16. (You may want to test this on an input relatively close to 1, such as 1.01.)

(define (two-hundred-fifty-sixth x)
(sixteenth (sixteenth x)))
Remark SCHEME provides built-in support for exponentiation (via the expt function, defined so that
(expt x y) yields x y ). For the exercises above, however, please construct the functions x 7 x k using
only and function application.
4. Reflect on your definition of two-hundred-fifty-sixth above. What would have been the difficulty of
defining this merely in terms of ?
Without abstraction (that is, using a subordinate function), this would have required us to write 256 occurrences of the variable x.

Вам также может понравиться