SICP 1.1

Key Concepts #

Environments #

Syntax #

Comparison between programming and natural languages #

Operator Evaluation Order #

Conditional Expressions / Predicates #

Exercise 1.1 #

Q: Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented.

Expression: 10
Result: 10
Expression: (+ 5 3 4)
Result: 12
Expression: (- 9 1)
Result: 8
Expression: (/ 6 2)
Result: 3
Expression: (+ (* 2 4) (- 4 6))
Result: 6
Expression: (define a 3)
Result: a?
Expression: (define b (+ a 1))
Result: b
Expresion: (+ a b (* a b))
Result: 19
Expression: (= a b)
Result: #f
Expression: (if (and (> b a) (< b (* a b)))
    b
    a)
Result: 4 (b)
Expression: (cond ((= a 4) 6)
      ((= b 4) (+ 6 7 a))
      (else 25))
Result: 16
Expression: (+ 2 (if (> b a) b a))
Result: 6
Expression: 
(* (cond ((> a b) a)
         ((< a b) b)
         (else -1))
   (+ a 1))
Result: 16

Exercise 1.2 #

Q: Translate the following expression into prefix form

   5 + 1/2 + (2 - (3 - (6 + 1/5)))/(3(6 - 2)(2 - 7)

A:

   (/ (+ 5 
     	 (/ 1 5)
      	 (- 2
      	    (- 3
	       (+ 6 (/ 1 5)))))
      (* 3
      	 (- 6 2)
	 (- 2 7)))

Exercise 1.3 #

Q: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

A:

(define (sum-of-larger-squares a b c)
	(cond ((and (> a c) (> b c)) (+ (sum-of-squares a b)))
	      ((and (> b a) (> c a)) (+ (sum-of-squares b c)))
	      (else (+ (sum-of-squares a c)))))

Exercise 1.4 #

Q: Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))

A: This function will return a - b if b is greater than 0 and a + b otherwise. This will always result in a value that is greater than a being returned. Hence, the name a-plus-abs-b.

Exercise 1.5 #

Q: Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:

(define (p) (p))

(define (test x y)
  (if (= x 0)
      0
      y))

Then he evaluates the expression

(test 0 (p))

What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)

A: With applicative-order evaluation, the this test will return the value of p, which will result in an infinitely recursive loop. With normal-order evaluation, we’ll return 0, since we don’t need the value of p.