Posts

Showing posts from October, 2017

SICP Exercise 2.6

Image
Exercise 2.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  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 ). SOLUTION

SICP Exercise 2.5

Exercise 2.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 . SOLUTION The code and tests are here .

SICP Exercise 2.4

Image
Exercise 2.4.   Here is an alternative procedural representation of pairs. For this representation, verify that  (car (cons x y))  yields  x  for any objects  x  and  y . (define (cons x y)   (lambda (m) (m x y))) (define (car z)   (z (lambda (p q) 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 .) SOLUTION

SICP Exercise 2.3 make-rectangle 2

Exercise 2.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? SOLUTION In this implementation, the rectangle is constructed given any two sides that share a common end point. If the segments do not satisfy all the required conditions, an error will be reported. The code and tests are here .

SICP Exercise 2.3 make-rectangle 1

Exercise 2.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? SOLUTION In this implementation, a rectangle is represented by its two diagonals. It is constructed by supplying two segments to it which will be its diagonals. If the segments do not satisfy the conditions to be diagonals of a rectangle, an error will be reported. The code and tests are here .

SICP Exercise 2.2 make-segment

Exercise 2.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  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 (y-point p))   (display ")")

SICP Exercise 2.1 make-rat support negative

Exercise 2.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. SOLUTION The code and tests are here .