Posts

Showing posts from June, 2018

SICP Exercise 2.80 generic zero? predicate

Exercise 2.80.    Define a generic predicate   =zero?   that tests if its argument is zero, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers. SOLUTION The code and tests are here .

SICP Exercise 2.79 generic equality predicate

Exercise 2.79.    Define a generic equality predicate   equ?   that tests the equality of two numbers, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers. SOLUTION The code and tests are here .

SICP Exercise 2.78 support for Scheme numbers

Exercise 2.78.    The internal procedures in the  scheme-number  package are essentially nothing more than calls to the primitive procedures  + ,  - , etc. It was not possible to use the primitives of the language directly because our type-tag system requires that each data object have a type attached to it. In fact, however, all Lisp implementations do have a type system, which they use internally. Primitive predicates such as  symbol?  and  number?  determine whether data objects have particular types. Modify the definitions of  type-tag ,  contents , and  attach-tag  from section  2.4.2  so that our generic system takes advantage of Scheme's internal type system. That is to say, the system should work as before except that ordinary numbers should be represented simply as Scheme numbers rather than as pairs whose  car  is the symbol  scheme-number . SOLUTION The code and tests are here .

SICP Exercise 2.77 magnitude

Exercise 2.77.   Louis Reasoner tries to evaluate the expression  (magnitude z)  where  z  is the object shown in figure  2.24 . To his surprise, instead of the answer 5 he gets an error message from  apply-generic , saying there is no method for the operation  magnitude  on the types  (complex) . He shows this interaction to Alyssa P. Hacker, who says ``The problem is that the complex-number selectors were never defined for  complex  numbers, just for  polar  and  rectangular  numbers. All you have to do to make this work is add the following to the  complex  package:'' (put 'real-part '(complex) real-part) (put 'imag-part '(complex) imag-part) (put 'magnitude '(complex) magnitude) (put 'angle '(complex) angle) Describe in detail why this works. As an example, trace through all the procedures called in evaluating the expression  (magnitude z)  where  z  is the object shown in figure  2.24 . In particular, how many times is  apply-generic  i

SICP Exercise 2.76 Comparison of the Three Strategies

Exercise 2.76.    As a large system with generic operations evolves, new types of data objects or new operations may be needed. For each of the three strategies -- generic operations with explicit dispatch, data-directed style, and message-passing-style -- describe the changes that must be made to a system in order to add new types or new operations. Which organization would be most appropriate for a system in which new types must often be added? Which would be most appropriate for a system in which new operations must often be added? SOLUTION Generic Operations with Explicit Dispatch Data-Directed Style Message-Passing Style Add New Types (as in polar, rectangular etc. in the case of complex numbers) 1.      Implement all the supported operations including the constructors under the new representation and tag the data with the new type. Make sure that these operation names do not conflict with any of the exi