Start DrRacket. Make sure that the language level is set to "Beginning Student" (Note: if you change the language level, it will not take effect until you hit the "Run" button).
Exercise 1: Experiment using DrRacket's Interactions Window as a calculator. See what operations you can use to calculate. Make sure to try big numbers and fractions. [3 min]
Exercise 2: Design a function that when given an integer that represents distance in miles will return the time (in hours) it takes to travel that distance when going 60 MPH. Write your function in the Definition Window, click Run, then use the Interactions Window to test your function. [5 mins]
Exercise 3: Design a function that given an integer representing time (in minutes) will return the distance traveled when going 70 MPH, rounding up to whole miles. [15 mins]
Hint: DrRacket comes with a help system called Help Desk. Open the Help Desk and click "How to Design Programs Languages", then click "Beginning Student". You will get a reference for the Beginning Student language. You can also highlight the name of a function and press F1, this will search Help Desk for the highlighted text.
Exercise 4: There are three kinds of errors you can make when writing a program in DrRacket. Come up with examples of each and try them out. Examine any error messages you get back. [5 mins]
Access the 2nd edition HTDP "universe" and "image" libraries in DrRacket. Put the following magic text at the top of your definitions window:
(require 2htdp/image) (require 2htdp/universe)
Libraries come with documentation. In the Help Desk, search for "universe". Also have a look at the documentation of the image library titled "2htdp/image".
Exercise 5: Download an image off the web and insert it
into your Definition window. Use define
to give
it a descriptive name.
Examples: cars, boats, planes etc.
Exercise 6: Use place-image
and
empty-scene
to create scenes with your image.
Exercise 7: Create rectangles with the following dimensions
Exercise 8: Design the function rec-sequence
that given an index will return the corresponding rectangle,
e.g., (rec-sequence 2)
returns a rectangle of size 20
by 50.
Using the "universe" module we can create animations. The module allows you to model a world. Every time the clock ticks, the module uses one of your functions to update or create a new world, which becomes the current world. The module can use another of your functions to create an image of your world.
;; World is a positive number (current time)
;; World -> World ;; calculates the next world (increments the time) (define (next-world w) (add1 w))
;; World -> Image ;; draw the image for snapshot time w in the middle ;; of a 200x200 scene (define (world-draw w) (place-image (circle w "solid" "black") 100 100 (empty-scene 200 200)))
big-bang
in Help Desk).
;; We start at time 1, use next-world to update the world at each ;; tick, and use world-draw to render the world as a scene: (big-bang 1 (on-tick next-world) (on-draw world-draw))
Exercise 9: Change the animation above so that instead of growing larger, the disk moves across the canvas. Try left to right (x), top to bottom (y) and diagonal (both x and y).
Exercise 10: Read the documentation of the "image" module and create an image of a car using circles and rectangles.
Exercise 11: Use the car image from Exercise 10 and create an animation with the car moving across the canvas.