Homework 11

Please read chapter 22 The Commerce of XML, 23 Simultaneous Processing, and 24 Summary in the textbook.

Be sure to include type, purpose, definition and tests for all functions.

Include the following at the top of your homework:

#lang typed/racket

(require "../include/uchicago151.rkt")
(require typed/test-engine/racket-tests)
(require typed/2htdp/image)
And include (test) at the bottom of your homework.

Problem 1

Use the following structure for expression trees:
(define-struct BinOp
  ([op : (U 'Plus 'Times)]
   [lsub : ExpTree]
   [rsub : ExpTree]))

(define-struct UnOp
  ([op : (U 'Negate 'Sqrt)]
   [sub : ExpTree]))

(define-type ExpTree (U Number UnOp BinOp))

We will use the above type ExpTree to represent an expression tree of numbers. A ExpTree can be one of three things: a Number, a unary operation with one subtree or a binary operation with two subtrees (left and right). Each operation is represented by a symbol:

Using these structures, write the following functions:

Problem 2

You will also need the following batch of data structures:

(define-struct Empty ())
(define-struct (Node A)
  ([value : A]
   [lsub : (BST A)]
   [rsub : (BST A)]))

(define-type (BST A) (U Empty (Node A)))

Write the following functions on polymorphic binary search trees. Use the structures and types above.

Problem 3

Use this interval structure:
(define-struct Interval
  ([min : Real]
   [max : Real]))

Write a function plotter according to this type ascription:

(: plot : (Real -> Real) Interval Interval Real Real -> Image)
The five arguments to this function are as follows:

As a hint to how to approach this problem, you might draw one one-pixel-wide vertical slice of the plot at a time, and put all the vertical slices together, left to right, with beside. That's the approach I followed. This strategy is reasonably simple to implement (though still challenging to get right) and the results, as you can see below, are good. Furthermore, you can start by using build-list to make a list of the x values, then use map to create a list of images each one pixel wide, then use foldr and beside to combine all those one-pixel-wide images into one big image.

Here is a gallery of sample plots to illustrate the behavior of plot. Please note I have used 'aliceblue as the background color of the plots, and 'blue for the color of the function itself; you need not use these colors.

(plot identity (Interval -100 100) (Interval -100 100) 1 1)

(plot identity (Interval -100 100) (Interval -100 100) 1/2 1)

(plot identity (Interval -100 100) (Interval -100 100) 1 1/2)

You will notice in this plot a "dotted line" quality -- it is fine for certain plots to look like that. In other words, the plot of the function need not be smoothly connected.

(plot (λ ([x : Real]) (* 1/5 (sqr x))) 
      (Interval -50 50) (Interval -100 100) 1/2 1)

The function should go outside the plot area as needed without perturbing the plot image.

(plot sin (Interval -100 100) (Interval -100 100) 1 1)

(plot sin (Interval -100 100) (Interval -2 2) 1 1)

(plot sin (Interval -10 10) (Interval -2 2) 1/16 1/16)

Fiddling with the extents and the scales is the way to make plots look right.

Problem 4

Write the f+, f* and fmax functions.

(: f+ : (Listof (Real -> Real)) -> (Real -> Real))

Return the function whose result is the sum of applying all the functions in the argument list. When the argument to f+ is the empty list, the return value is the constant function that always returns 0.

(plot (f+ (list sin identity)) (Interval -50 50) (Interval -50 50) 1/2 1/2)

(: f* : (Listof (Real -> Real)) -> (Real -> Real))

Return the function whose result is the product of applying all the functions in the argument list. When the argument to f* is the empty list, the return value is the constant function that always returns 1.

(plot (f* (list sin identity)) (Interval -20 20) (Interval -20 20) 1/5 1/2)

(: fmax : (Listof (Real -> Real)) -> (Real -> Real))

Return the function whose result is the maximum of applying all the functions in the argument list. When the argument to fmax is the empty list, raise an error, since fmax is undefined on the empty list of functions. When the argument is a singleton list of functions, just return that function.

(plot (fmax (list sin cos)) (Interval -10 10) (Interval -2 2) 1/20 1/20)

Practice

The quiz Friday will be mostly about trees, lambda, match, and higher-order functions (map, filter, fold, build-list).

Submit your work in your repository in hw11/hw11.rkt by noon Friday, July 14.