SICP in Clojure - set-1
http://sicpinclojure.com/?q=taxonomy/term/31/0
enExercise 2.91
http://sicpinclojure.com/?q=exercise/exercise-2-91
<p>A univariate polynomial can be divided by another one to produce a polynomial quotient and a polynomial remainder. For example,</p>
<p><img src="/sites/default/files/images/ch2-Z-G-78_0.gif" alt="" title="" width="225" height="33" /></p>
<p>Division can be performed via long division. That is, divide the highest-order term of the dividend by the highest-order term of the divisor. The result is the first term of the
quotient. Next, multiply the result by the divisor, subtract that from the dividend, and produce the rest of the answer by recursively dividing the difference by the divisor. Stop when the order of the divisor exceeds the order of the dividend and declare the dividend to
be the remainder. Also, if the dividend ever becomes zero, return zero as both quotient and remainder.</p>
<p>We can design a <code class="scheme">div-poly</code> procedure on the model of <code class="scheme">add-poly</code> and <code class="scheme">mul-poly</code>. The procedure checks to see if the two polys have the same variable. If so, <code class="scheme">div-poly</code> strips off the variable and passes the problem to <code class="scheme">div-terms</code>, which performs the division operation on term lists. <code class="scheme">Div-poly</code> finally reattaches the variable to the result supplied by <code class="scheme">div-terms</code>. It is convenient to design <code class="scheme">div-terms</code> to compute both the quotient and the remainder
of a division. <code class="scheme">Div-terms</code> can take two term lists as arguments and return a list of the quotient term list and the remainder term list.</p>
<p>Complete the following definition of <code class="scheme">div-terms</code> by filling in the missing expressions. Use this to implement <code class="scheme">div-poly</code>, which takes two polys as arguments and returns a list of the quotient and
remainder polys.</p>
<pre><code class="scheme">(define (div-terms L1 L2)
(if (empty-termlist? L1)
(list (the-empty-termlist) (the-empty-termlist))
(let ((t1 (first-term L1))
(t2 (first-term L2)))
(if (> (order t2) (order t1))
(list (the-empty-termlist) L1)
(let ((new-c (div (coeff t1) (coeff t2)))
(new-o (- (order t1) (order t2))))
(let ((rest-of-result
<<var>compute rest of result recursively</var>>
))
<<var>form complete result</var>>
))))))</code></pre><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-5-3-example-symbolic-algebra">2.5.3 Example: Symbolic Algebra</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-91#commentsset-1Mon, 20 Jul 2009 06:22:49 +0000Corey White276 at http://sicpinclojure.comExercise 2.90
http://sicpinclojure.com/?q=exercise/exercise-2-90
<p>Suppose we want to have a polynomial system that is efficient for both sparse and dense polynomials. One way to do this is to allow both kinds of term-list representations in our system. The situation is analogous to the complex-number example of <a href="2-4-multiple-representations-abstract-data">section 2.4</a>, where we allowed both rectangular and polar representations. To do this we must distinguish different types of term lists and make the operations on term lists generic. Redesign the polynomial system to implement this generalization. This is a major effort, not a local change.</p><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-5-3-example-symbolic-algebra">2.5.3 Example: Symbolic Algebra</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-90#commentsset-1Mon, 20 Jul 2009 06:18:20 +0000Corey White275 at http://sicpinclojure.comExercise 2.89
http://sicpinclojure.com/?q=exercise/exercise-2-89
<p>Define procedures that implement the term-list representation described above as appropriate for dense polynomials.</p><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-5-3-example-symbolic-algebra">2.5.3 Example: Symbolic Algebra</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-89#commentsset-1Mon, 20 Jul 2009 06:17:05 +0000Corey White274 at http://sicpinclojure.comExercise 2.88
http://sicpinclojure.com/?q=exercise/exercise-2-88
<p>Extend the polynomial system to include subtraction of polynomials. (Hint: You may find it helpful to define a generic negation operation.)</p><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-5-3-example-symbolic-algebra">2.5.3 Example: Symbolic Algebra</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-88#commentsset-1Mon, 20 Jul 2009 06:16:30 +0000Corey White273 at http://sicpinclojure.comExercise 2.87
http://sicpinclojure.com/?q=exercise/exercise-2-87
<p>Install <code class="scheme">=zero?</code> for polynomials in the generic arithmetic package. This will allow <code>adjoin-term</code> to work for polynomials with coefficients that are themselves polynomials.</p><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-5-3-example-symbolic-algebra">2.5.3 Example: Symbolic Algebra</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-87#commentsset-1Mon, 20 Jul 2009 06:15:53 +0000Corey White272 at http://sicpinclojure.comExercise 2.74
http://sicpinclojure.com/?q=exercise/exercise-2-74
<p>Insatiable Enterprises, Inc., is a highly decentralized conglomerate company consisting of a large number of independent divisions located all over the world. The company’s computer facilities have just been interconnected by means of a clever network-interfacing scheme that makes the entire network appear to any user to be a single computer. Insatiable’s president, in her first attempt to exploit the ability of the network to extract administrative information from division files, is dismayed to discover that, although all the division files have been implemented as data structures in Scheme, the particular data structure used varies from division to division. A meeting of division managers is hastily called to search for a strategy to integrate the files that will satisfy headquarters’ needs while preserving the existing autonomy of the divisions.</p>
<p>Show how such a strategy can be implemented with data-directed programming. As an example, suppose that each division’s personnel records consist of a single file, which contains a set of records keyed on employees’ names. The structure of the set varies from
division to division. Furthermore, each employee’s record is itself a set (structured differently from division to division) that contains information keyed under identifiers such as <code class="scheme">address</code> and <code class="scheme">salary</code>. In particular:</p>
<ol class="alphabetical">
<li><p>Implement for headquarters a <code class="scheme">get-record</code> procedure that retrieves a specified employee’s record from a specified personnel file. The procedure should be applicable to any division’s file. Explain how the individual divisions’ files should be structured. In particular, what type information must be supplied?</p></li>
<li><p>Implement for headquarters a <code class="scheme">get-salary</code> procedure that returns the salary information from a given employee’s record from any division’s personnel file. How should the record be structured in order to make this operation work?</p></li>
<li><p>Implement for headquarters a <code class="scheme">find-employee-record</code> procedure. This should search all the divisions’ files for the record of a given employee and return the record. Assume that this procedure takes as arguments an employee’s name and a list of all the divisions’ files.</p></li>
<li><p>When Insatiable takes over a new company, what changes must
be made in order to incorporate the new personnel information into the
central system?</p></li>
</ol><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-4-3-data-directed-programming-and-additivity">2.4.3 Data-Directed Programming and Additivity</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-74#commentsset-1Mon, 20 Jul 2009 01:34:45 +0000Corey White260 at http://sicpinclojure.comExercise 2.73
http://sicpinclojure.com/?q=exercise/exercise-2-73
<p><a href="http://sicpinclojure.com/sicp/2-3-2-example-symbolic-differentiation">Section 2.3.2</a> described a program that performs symbolic differentiation:</p>
<pre><code class="scheme">(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
((sum? exp)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var)))
((product? exp)
(make-sum
(make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp))))
<<var>more rules can be added here</var>>
(else (error "unknown expression type -- <span class="caps">DERIV</span>" exp))))</code></pre>
<p>We can regard this program as performing a dispatch on the type of the expression to be differentiated. In this situation the “type tag” of the datum is the algebraic operator symbol (such as <code class="scheme">+</code>) and the operation being performed is <code class="scheme">deriv</code>. We can transform this program into data-directed style by rewriting the basic derivative procedure as</p>
<pre><code class="scheme">(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
(else ((get 'deriv (operator exp)) (operands exp)
var))))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))</code></pre>
<ol class="alphabetical">
<li><p>Explain what was done above. Why can’t we assimilate the predicates <code class="scheme">number?</code> and <code class="scheme">same-variable?</code> into the data-directed dispatch?</p></li>
<li><p>Write the procedures for derivatives of sums and products, and the
auxiliary code required to install them in the table used by the
program above.</p></li>
<li><p>Choose any additional differentiation rule that you like, such as the one for exponents (<a href="http://sicpinclojure.com/exercise/exercise-2-56">exercise 2.56</a>), and install it in this data-directed system.</p></li>
<li><p>In this simple algebraic manipulator the type of an expression is the algebraic operator that binds it together. Suppose, however, we indexed the procedures in the opposite way, so that the dispatch line in <code class="scheme">deriv</code> looked like</p>
<pre><code class="scheme">((get (operator exp) 'deriv) (operands exp) var)</code></pre>
<p>What corresponding changes to the derivative system are required?</p></li>
</ol><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-4-3-data-directed-programming-and-additivity">2.4.3 Data-Directed Programming and Additivity</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-73#commentsset-1Mon, 20 Jul 2009 01:23:51 +0000Corey White259 at http://sicpinclojure.comExercise 2.62
http://sicpinclojure.com/?q=exercise/exercise-2-62
<p>Give a Θ(<var>n</var>) implementation of <code class="scheme">union-set</code> for sets represented as ordered lists.</p><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-3-3-example-representing-sets">2.3.3 Example: Representing Sets</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-62#commentsset-1Sun, 19 Jul 2009 03:28:13 +0000Corey White248 at http://sicpinclojure.comExercise 2.61
http://sicpinclojure.com/?q=exercise/exercise-2-61
<p>Give an implementation of <code class="scheme">adjoin-set</code> using the ordered
representation. By analogy with <code class="scheme">element-of-set?</code> show how to
take advantage of the ordering to produce a procedure that requires on the average about half as many steps as with the unordered representation.</p><div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-3-3-example-representing-sets">2.3.3 Example: Representing Sets</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-61#commentsset-1Sun, 19 Jul 2009 03:27:26 +0000Corey White247 at http://sicpinclojure.comExercise 2.44
http://sicpinclojure.com/?q=exercise/exercise-2-44
<p>Define the procedure <code class="scheme">up-split</code> used by <code class="scheme">corner-split</code>.
It is similar to <code class="scheme">right-split</code>, except that it switches the
roles of <code class="scheme">below</code> and <code class="scheme">beside</code>.</p>
<div class="field field-type-nodereference field-field-corresponding-section">
<div class="field-label">Corresponding Section: </div>
<div class="field-items">
<div class="field-item odd">
<a href="/?q=sicp/2-2-4-example-picture-language">2.2.4 Example: A Picture Language</a> </div>
</div>
</div>
http://sicpinclojure.com/?q=exercise/exercise-2-44#commentsset-1Sat, 18 Jul 2009 17:47:43 +0000Corey White232 at http://sicpinclojure.com