• Home
  • Textbooks
  • Structure and Interpretation of Computer Programs
  • Building Abstractions with Data

Structure and Interpretation of Computer Programs

Harold Abelson, Gerald Jay Sussman

Chapter 2

Building Abstractions with Data - all with Video Answers

Educators


Section 1

Introduction to Data Abstraction

04:05

Problem 1

Define a better version of make-rat that handles both positive and negative arguments. Make-rat should normalize the sign so that if the rational number is positive, both the numerator and denominator are positive, and if the rational number is negative, only the numerator is negative.

John Bell
John Bell
Numerade Educator
02:00

Problem 2

Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the $x$ coordinate and the $y$ coordinate. Accordingly, specify a constructor make-point and selectors $\mathrm{x}$-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints). To try your procedures, you'll need a way to print points:
(define (print-point p)
(newline)
(display " (")
(display (x-point p))
(display ",")
(display ( $\mathrm{y}$-point $\mathrm{p}$ ))
(display ")"))

Mike Gaerlan
Mike Gaerlan
Numerade Educator
03:59

Problem 3

Implement a representation for rectangles in a plane. (Hint: You may want to make use of exercise 2.2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so that the same perimeter and area procedures will work using either representation?

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:26

Problem 4

Here is an alternative procedural representation of pairs. For this representation, verify that (car (cons $\mathrm{x} \mathrm{y}$ )) yields $\mathrm{x}$ for any objects $\mathrm{x}$ and $\mathrm{y}$.
(define (cons $\mathrm{x} \mathrm{y}$ )
$\quad($ lambda $(\mathrm{m})(\mathrm{m} \mathrm{x} \mathrm{y})))$
$($ define $(\mathrm{car} \mathrm{z})$
$(\mathrm{z}($ lambda $(\mathrm{p} \mathrm{q}) \mathrm{p})))$
What is the corresponding definition of cdr? (Hint: To verify that this works, make use of the substitution model of section 1.1.5.)

Harmender Singh Yadav
Harmender Singh Yadav
Numerade Educator
01:49

Problem 5

Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair $a$ and $b$ as the integer that is the product $2^{a} 3^{b}$. Give the corresponding definitions of the procedures cons, car, and cdr.

James Chok
James Chok
Numerade Educator
01:04

Problem 6

In case representing pairs as procedures wasn't mind-boggling enough, consider that, in a language that can manipulate procedures, we can get by without numbers (at least insofar as nonnegative integers are concerned) by implementing 0 and the operation of adding 1 as
(define zero (lambda (f) (lambda ( $x$ ) $x$ )))
(define (add-1 ${n}$ )
(lambda (f) (lambda ( $x$ ) (f ( $($ n $f) x))$ ))
This representation is known as Church numerals, after its inventor, Alonzo Church, the logician who invented the $\lambda$ calculus.

Define one and two directly (not in terms of zero and add-1). (Hint: Use substitution to evaluate (add-1 zero)). Give a direct definition of the addition procedure + (not in terms of repeated application of add-1).

Dale Sanford
Dale Sanford
Numerade Educator
00:36

Problem 7

Alyssa's program is incomplete because she has not specified the implementation of the interval abstraction. Here is a definition of the interval constructor:
(define (make-interval a b) (cons a b))
Define selectors upper-bound and lower-bound to complete the implementation.

Nick Johnson
Nick Johnson
Numerade Educator
01:57

Problem 8

Using reasoning analogous to Alyssa's, describe how the difference of two intervals may be computed. Define a corresponding subtraction procedure, called sub-interval.

Adriano Chikande
Adriano Chikande
Numerade Educator
02:32

Problem 9

The width of an interval is half of the difference between its upper and lower bounds. The width is a measure of the uncertainty of the number specified by the interval. For some arithmetic operations the width of the result of combining two intervals is a function only of the widths of the argument intervals, whereas for others the width of the combination is not a function of the widths of the argument intervals. Show that the width of the sum (or difference) of two intervals is a function only of the widths of the intervals being added (or subtracted). Give examples to show that this is not true for multiplication or division.

Ahmad Reda
Ahmad Reda
Numerade Educator
01:20

Problem 10

Ben Bitdiddle, an expert systems programmer, looks over Alyssa's shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Modify Alyssa's code to check for this condition and to signal an error if it occurs.

Jack Chen
Jack Chen
Numerade Educator
05:18

Problem 11

In passing, Ben also cryptically comments: "By testing the signs of the endpoints of the intervals, it is possible to break mul-interval into nine cases, only one of which requires more than two multiplications." Rewrite this procedure using Ben's suggestion.

After debugging her program, Alyssa shows it to a potential user, who complains that her program solves the wrong problem. He wants a program that can deal with numbers represented as a center value and an additive tolerance; for example, he wants to work with intervals such as $3.5 \pm 0.15$ rather than $[3.35,3.65]$. Alyssa returns to her desk and fixes this problem by supplying an alternate constructor and alternate selectors:
(define (make-center-width $c$ w)
(make-interval $(-c$ w) $(+c$ w) $)$ )
(define (center i)
(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)
(/ (- (upper-bound i) (lower-bound i)) 2))
Unfortunately, most of Alyssa's users are engineers. Real engineering situations usually involve measurements with only a small uncertainty, measured as the ratio of the width of the interval to the midpoint of the interval. Engineers usually specify percentage tolerances on the parameters of devices, as in the resistor specifications given earlier.

Stanley Enemuo
Stanley Enemuo
Numerade Educator
00:52

Problem 12

Define a constructor make-center-percent that takes a center and a percentage tolerance and produces the desired interval. You must also define a selector percent that produces the percentage tolerance for a given interval. The center selector is the same as the one shown above.

Allison Knapp
Allison Knapp
Numerade Educator
View

Problem 13

Show that under the assumption of small percentage tolerances there is a simple formula for the approximate percentage tolerance of the product of two intervals in terms of the tolerances of the factors. You may simplify the problem by assuming that all numbers are positive.

Victor Salazar
Victor Salazar
Numerade Educator
01:22

Problem 14

Demonstrate that Lem is right. Investigate the behavior of the system on a variety of arithmetic expressions. Make some intervals $A$ and $B$, and use them in computing the expressions $A / A$ and $A / B$. You will get the most insight by using intervals whose width is a small percentage of the center value. Examine the results of the computation in center-percent form (see exercise 2.12).

AG
Ankit Gupta
Numerade Educator
10:15

Problem 15

Eva Lu Ator, another user, has also noticed the different intervals computed by different but algebraically equivalent expressions. She says that a formula to compute with intervals using Alyssa's system will produce tighter error bounds if it can be written in such a form that no variable that represents an uncertain number is repeated. Thus, she says, par2 is a "better" program for parallel resistances than par1. Is she right? Why?

Oswaldo Jiménez
Oswaldo Jiménez
Numerade Educator
00:30

Problem 16

Explain, in general, why equivalent algebraic expressions may lead to different answers. Can you devise an interval-arithmetic package that does not have this shortcoming, or is this task impossible? (Waming: This problem is very difficult.)

Joanna Quigley
Joanna Quigley
Numerade Educator