Homework 2

Please read chapter 3 and 4 in the textbook. I also want to draw your attention to the link to the Typed Racket Notes. You should find the first two parts ("Getting Started" and "Functions and definitions") quite helpful. The other sections refer to features we haven't learned yet, but we'll get there.

Usually, I will have a homework due every Monday, Wednesday and Friday. Because of the extensions for hw1 and lab1 and because I was delayed in releasing this homework, hw2 will not be due until Sunday. However, hw3 will still be assigned Friday and due Monday, so please do not leave this homework until the last minute.

All problems are to be done using DrRacket. You should be using typed racket, meaning that the first line of your definitions window should be #lang typed/racket.

When working on these problems, you should experiment in the interactions window (the bottom part of the screen). Once you have a solution to a problem, you should copy it to the definitions window (the top part of the screen). To save your work, use File > Save Definitions or File > Save Definitions As to save what's written in the definitions. There is no way to save what's in the interactions window - it is just a place to experiment and test your code. Once you are done, click the Run button to test your code and check the results that appear in the interactions window.

Include the following at the top of your homework:

#lang typed/racket

(require typed/2htdp/image)
(require typed/2htdp/universe)
(: WIDTH Nonnegative-Integer)
(define WIDTH 50)

(: HEIGHT Nonnegative-Integer)
(define HEIGHT 200)

(: RADIUS Nonnegative-Integer)
(define RADIUS 10)

Problem 1

Modify Problem 2 from Lab 1 so that it uses the variables WIDTH, HEIGHT and RADIUS instead of the numbers themselves. You should simply be able to copy your solution from Lab 1 and replace the numbers with the variables. If you change the variables, the image should change appropriately.

Problem 2

In the same style as WIDTH, HEIGHT, and RADIUS above, define variables A, B, and C with type Real as follows:

A = -0.1
B = 8.48
C = 0

Then, define the following function with type Real -> Real:

Problem 3

Write a function draw-at-time. It will take a Real, t, as input and produce an image as output. It will compute the height of a ball at time t according to the equation in Problem 2 and then create an image of the ball at that time according to the method in Problem 1. Your answer should be very short and simple. You should rely on the functions that you already wrote for Problems 1 and 2.

Problem 4

Write a function below-ground that takes a Real t as input and returns true when height(t) is less than zero and returns false otherwise.

Problem 5

Type the following into your definitions window:


(big-bang 0 : Real
          [to-draw draw-at-time]
          [on-tick add1]
          [stop-when below-ground])

You should see an animation of a ball rising and falling. The big-bang function is a built in funciton for doing animation. The initial state is the first varialbe (in our case the initial time, 0). The part that comes after to-draw is a function that will be used to create one frame of the animation using the current state as input. The part after on-tick is the function that is used to go from the state at one frame to the state at the next one. In our example, the time goes from 0 to 1 to 2 to 3 and so on. The function add1 takes one state (the current time) and produces the next state (the next time). The stop-when part tells the animation when to stop. In our case, it stops at the time when the ball passes below ground.

Submit your work in your repository in hw2/hw2.rkt by 6pm Sunday, June 25.