Make sure you start your file with:
#lang htdp/bsl (require 2htdp/image) (require 2htdp/universe)
For each math expression below, translate it to Racket.
expt function, division to /, and multiplication to *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.
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)
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.