<week title="Designing functions for arbitrarily large data (2)">
<lectures dates="9/28 or 29">HtDP: Part III</lectures>
<assignment due="10/07">

<p><b>Purpose:</b></p>

<blockquote>
The goal of this problem set is to help you design functions that deal with
arbitrarily large data, especially trees, forests of trees and trees of
forests, and forests of forests.
</blockquote>

<!-- ================================================================== -->
<hr />

<h4>Drill:</h4>

<blockquote>
HtDP: 14.1.4, 14.1.6, 14.2.1, 14.2.2, 14.2.3, 14.2.4, 17.6.4, 17.6.6,
17.7.1, 17.7.2, 17.7.3, 17.7.4
</blockquote>

<!-- ================================================================== -->
<hr />

<h4>Required Problems:</h4>

<p><strong>Note:</strong> You must use DrScheme's HtDP Intermediate Student
  Language.</p>

<ol>
<li>
<!-- ================================================================== -->
<!-- PROBLEM 1 -->

<p>Economists use "choice trees" to record how decisions are made in
   so-called binary situations:
<pre><code>    (define-struct choice (left right))
    ;; An ChoiceTree is one of: 
    ;; -- Number 
    ;; -- (make-choice ChoiceTree ChoiceTree)
</code></pre>
   The weight of a <code>ChoiceTree</code> is the sum of its numbers. 
</p>

<p>
 For the analysis of <code>ChoiceTree</code>s we need two functions: 

 <ol>

   <li><code>balanced?</code>, which determines whether a
    <code>ChoiceTree</code> is balanced. 

    <p>
    <strong>Definition:</strong> A balanced choice tree contains only instances of
    <code>choice</code> structures whose two subtrees have the same weight.
    </p>
   </li>

   <li><code>decide</code>, which consumes an <code>ChoiceTree</code> and a list of
    <code>Directions</code>s to find a final decision---a leaf---in the
    tree. 
    <p>
      <strong>Definition:</strong> A <code>Direction</code> is one of:
      <code>"left"</code> or <code>"right"</code>. 
    </p>
    
    Naturally, <code>decide</code> works just like a person who is given a
    list of directions in a city. When, however, the function exhausts the
    list and hasn't discovered a leaf yet, it signals an error. Similarly,
    it is also an error if the function reaches a leaf and there are some
    directions left. 

   </li>
 </ol>
</p>
</li>

<!-- ================================================================== -->
<!-- PROBLEM 2 -->

<li>Design the function <code>sumF</code>, which adds up all the numbers
  that occur in a forest. Here are the relevant data definitions: 
<pre><code>    ;; A Forest is one of: 
    ;;  -- empty 
    ;;  -- (cons Tree Forest)
    ;; A Tree is one of: 
    ;;  -- Number 
    ;;  -- (cons Forest (cons Number (cons Forest empty)))
</code></pre>
</li>

<!-- ================================================================== -->
<!-- PROBLEM 3 -->

<li>Design a program that renders an <code>Expr</code> as an image: 
<pre><code>     (define-struct pls (x in?))
     (define-struct mul (x in?))
     ;; Expr is one of:
     ;; -- Number
     ;; -- (make-pls (cons Expr 1LON) Boolean)
     ;; -- (make-mul (cons Expr 1LON) Boolean)
     ;; 1LON is one of: 
     ;; -- (cons Expr empty)
     ;; -- (cons Expr 1LON)
     ;; interp. a <code>pls</code> struct represents an addition expression
     ;; with at least two sub-expressions; a <code>mul</code> represents a 
     ;; multiplication; the boolean flag indicates whether it 
     ;; represents an infix or a prefix expression. 
</code></pre>
 The rendering program consumes an <code>Expr</code> and produces an image. 
 It ignores the boolean flags and instead renders all expressions as prefix
 expressions. 

<p>Here are two examples: 
<table border="1" align="center">
<tr><td><pre><code><font size="-1">(make-pls (list 1 3 2) true)
        </font></code></pre></td>
     <td><img src="3expr-pls.png"  alt="addition"/></td> 
</tr>
<tr><td><pre><code><font size="-1">(make-mul (list (make-pls (list 1 3 2) true)
                (make-mul (list 1 3 2) true))
          false)</font></code></pre></td>
     <td><img src="3expr.png"  alt="nested"/></td>
</tr>
</table>
Hint: This problem is related to problems 1.3 and 2.3. 
</p>

<p><strong>Domain knowledge:</strong> 
 The rendering places only the first sub-expression of an addition or
 multiplication on the same line as the operator. All others are
 on separate lines below the first line, always indented by five pixels.
 The closing parentheses of an expression appears on its last line.
</p>
</li>

<!-- ================================================================== -->
<!-- PROBLEM 4 -->

<li> <em>Mini Project:</em>

Design a Space Invader program. 

<p>
 The player is in control of a ``tank'' (small rectangle) that must defend
 earth (the bottom of the canvas) from a UFO (``flying saucer'') that
 descends from the top of the canvas to the bottom. In order to stop the
 UFO from landing, the player may fire missiles (triangles, smaller than
 the ``tank''). To fire a missile, the player hits the space bar and, in
 response, the missile emerges from the tank. If the UFO collides with the
 missile, the player wins; otherwise the UFO lands and the player and
 ``earth'' is lost.
</p>

<p>
 Here are some details concerning the movement of the three game
 objects. First, the tank moves a constant velocity along the bottom of the
 canvas. The player may use the left arrow key and the right arrow key to
 change directions. Second, the UFO descends at a constant speed but makes
 small random jumps to the left or right---without ever disappearing from
 the canvas. Third, once fired a missile ascends along a straight vertical
 line at a constant speed at least twice as fast as the UFO
 descends. Finally, the UFO and the missile ``collide'' if their reference
 points are close enough---for whatever you think ``close enough'' should
 mean.
</p>

<p>
 BSL and ISL come with the function 
<pre><code>
     ;; random : NaturalNumber -> NaturalNumber 
     ;; <code>(random n)</code> produces a number in <code>[0,n)</code>
     (define (random n) 
       ;; magic happens here 
       ...)
</code></pre>
 Use this function to move the UFO randomly, but remember to always keep
 the UFO inside the canvas's boundaries. -- To test functions that employ
<code>random</code>, create a main function that calls an auxiliary
 function.  The auxiliary function should <strong>not</strong> use
<code>random</code> and should perform all computations. Instead the main
 function should call the auxiliary function on random numbers. 
 That way you can still test the auxiliary function where all the real
 computation happens.  
</p>

</li>
</ol>

</assignment>
</week>
