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.
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
Note: You must use DrScheme's HtDP Intermediate Student Language.
Economists use "choice trees" to record how decisions are made in so-called binary situations:
The weight of a
(define-struct choice (left right)) ;; An ChoiceTree is one of: ;; -- Number ;; -- (make-choice ChoiceTree ChoiceTree)
ChoiceTreeis the sum of its numbers.
For the analysis of
ChoiceTrees we need two functions:
balanced?, which determines whether a
Definition: A balanced choice tree contains only instances of
choice structures whose two subtrees have the same weight.
decide, which consumes an
ChoiceTreeand a list of
Directionss to find a final decision---a leaf---in the tree.
Direction is one of:
decideworks 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.
sumF, which adds up all the numbers that occur in a forest. Here are the relevant data definitions:
;; A Forest is one of: ;; -- empty ;; -- (cons Tree Forest) ;; A Tree is one of: ;; -- Number ;; -- (cons Forest (cons Number (cons Forest empty)))
Expras an image:
The rendering program consumes an
(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
plsstruct represents an addition expression ;; with at least two sub-expressions; a
mulrepresents a ;; multiplication; the boolean flag indicates whether it ;; represents an infix or a prefix expression.
Exprand produces an image. It ignores the boolean flags and instead renders all expressions as prefix expressions.
Here are two examples:
Domain knowledge: 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.
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.
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.
BSL and ISL come with the function
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
;; random : NaturalNumber -> NaturalNumber ;;
(random n)produces a number in
[0,n)(define (random n) ;; magic happens here ...)
random, create a main function that calls an auxiliary function. The auxiliary function should not use
randomand 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.