Arithmetic, Functions, and Animations

Make sure you start your file with:

#lang htdp/bsl
(require 2htdp/image)
(require 2htdp/universe)
Part A. Racket Math

For each math expression below, translate it to Racket.

Example:

Expression: \(3 + 2\cdot 5^{2}\)

What you’d put in DrRacket’s Definitions window:

; Expression: 3 + 2 * 5 ^ 2
; Parenthesized: (3 + (2 * (5 ^ 2)))
; Simplified: 53
; Racket: (+ 3 (* 2 (expt 5 2)))
(check-expect (+ 3 (* 2 (expt 5 2))) 53)

Hit Run after you write each exercise to make sure your answer matches Racket’s. If it doesn’t, figure out what went wrong.

  1. \(4\cdot 3^2-5\)
  2. \(2^3\cdot 4 - 7\cdot 3\)
  3. \(18\div 3\cdot 2 + 7\)
  4. \((6 + 3) - (4\cdot 8) + 5\)
  5. \((5 + 1) \cdot 4 - (20 \div 5) + 3^{2 + 1}\)
Part B. Functions

For each function defined below, create a T-chart for the domain values -2, -1, 0, 1, and 2. (You can put the function and chart in a comment between #| and |#.) Then write the function and five test cases to make sure you translated it correctly.

For example, if the problem were \(f(x) = 3x^2 - 1\), you would write:

#|
f(x) = 3x^2 - 1
 x  | f(x)
----+------
 -2 | 11
 -1 | 2
 0  | -1
 1  | 2
 2  | 11
|#

(define (f x)
  (- (* 3 (expt x 2)) 1))

(check-expect (f -2) 11)
(check-expect (f -1) 2)
(check-expect (f 0) -1)
(check-expect (f 1) 2)
(check-expect (f 2) 11)
  1. \(g(x) = 3x - 5\)
  2. \(d(x) = x^3\)
  3. \(s(k) = k^2 + 2k - 1\)
  4. \(h(z) = 3z^3 - z^2 + 2z - 5\)
  5. \(p(n) = \frac{n(n - 1)}{2}\)
Part C. Animations

Here’s the starter code for animating a circle.

(define (circle-pic t)
  (place-image (circle 15 "solid" "purple")
               t 125
               (empty-scene 250 250)))

(animate circle-pic)

This makes a purple circle move from left to right in the middle of a 250 pixel by 250 pixel scene. For each description below, change the function so that the animation changes as described.

You can just name each function with its number: circle-pic-1, circle-pic-2, etc. Don’t forget to add the right number in the animate function or nothing will change.

  1. Change the color of the circle.
  2. Make the circle have a radius of 40 instead of 15.
  3. Make the circle go across the middle of a 400x400 pixel scene.
  4. Make the circle go down the middle of the scene.
  5. Make the circle start at the bottom left and go up to the top right.
  6. Challenge: See if you can get two (or more) circles moving on the screen at the same time.
  7. Change the animation in some way that seems cool to you.