Start DrScheme. Make sure that the language level is set to "Beginning Student" (if you change the language level, it will not take effect until you hit the "Run" button).
Experiment with using DrScheme's Interactions window as a calculator. See what operations you can use. Try big numbers and fractions. Repeat the same calculations using the Definitions window and the "Run" button. Use the Stepper to see how DrScheme evaluates the expressions in the Definitions window.
Design a function that given an integer that represents time in hours returns back the time in minutes. Follow the design recipe. Give to the function a descriptive name. Before coding the body of the function, make sure you have examples.
Untested code shows up in a different color.
Turn your examples into tests using
check-expect. Write enough tests to eliminate any ugly
Experiment with errors. There are three kinds of errors you can make when writing a program in DrScheme.
In PDP we practice pair programming. One partner, the pilot, has control of the keyboard. The other partner, the co-pilot, looks over the pilot's shoulder. The pilot and co-pilot exchange roles periodically. The pilot and co-pilot discuss every detail of their code and they are both equally responsible for it.
We expect that you practice pair programming as much as possible this semester. Make sure that you and your partner have compatible schedules and exchange all the necessary information you need to arrange your meetings. Pairs will be shuffled approximatelly every 3-4 weeks.
Now work with your partners.
DrScheme comes with a collection of useful libraries called teachpacks.
The first one we use is the Universe teachpack. To enable the
teachpack, add the line
(require 2htdp/universe) at the top of
the Definitions window.
Teachpacks come with documentation. Search for "Universe" in the documentation and click the "2htdp/universe module" link.
Download an image off the web and insert it
into your Definitions window. Use
define to give
it a descriptive name.
empty-scene to create scenes with your image.
Create rectangles with the following dimensions
rec-sequencethat given an index within the series of rectangles will return the corresponding solid yellow rectangle at that index, e.g.,
(rec-sequence 2)returns a rectangle of size 4 by 8. Make sure to write enough tests for your function. A test for
rec-sequencewould look like:
(check-expect (rec-sequence 2) (rectangle 4 8 "solid" "yellow"))
Using the Universe teachpack we can create animations. The teachpack allows us to model a world. Every time the clock ticks, the teachpack uses one of our functions to change the state of the world. The teachpack uses another of our functions to create an image of our world.
;; WorldState = Number (positive)
;; WorldState -> WorldStete ;; calculates the next state of the world (define (next-world w) (add1 w))
;; WorldState -> Scene ;; draws the picture of the world with state w (define (world-draw w) (place-image (circle w "solid" "black") 100 100 (empty-scene 200 200)))
;; WorldState -> WordState ;; we start the animation with initial circle radius w ;; we set next-world as the clock handler and ;; and world-draw as the draw handler (define (start w) (big-bang w (on-tick next-world 0.01) (on-draw world-draw)))
Call start with an an initial radius at the Interactions window to start the animation.
Change the animation above so that the disk moves across the canvas. Try left to right, top to bottom and diagonal.
Modify your solution to the previous exercise so that the direction of the disk changes when pressing one of the arrow keys.
In PDP, we use subversion to submit hw solutions. For a quick overview of subversion and its main commands, take a look at the svn guide handout.
For today's lab, you are provided with a temporary username and password. This account will be deleted after the end of the lab sessions. For hw solutions submission, use your ccis account.
Check out your pair's folder. Find the url of the repository at the svn guide handout.
Add the files with your solutions to the lab exercises to your pair's folder and commit. Check out your folder from another computer. Make sure that the new files are in the folder after check out.
Modify the files in your folder and commit again. Update the folder at the other computer you cheked out your folder and make sure that you can see the modified version of the files.
Design a program that simulates a US traffic light with the Universe teachpack.
A traffic light that is "off" should be rendered as three colored circles; if one of the bulbs is supposed to be "on", render the corresponding position as a solidly colored disk. For simplicity, make each phase last one second. Make sure that you can change the size of the traffic light with a single change to your program.
The following program draws a simple light with the green bulb "on". Note the definition of descriptive constants to make the program more readable and avoid the repetition of "magic" constants.
(require 2htdp/universe) ;; scene size (define SCENE-SIZE 200) ;; light and parts dimensions (define LIGHT-BOX-HEIGHT (/ SCENE-SIZE 2)) (define LIGHT-BOX-WIDTH (/ SCENE-SIZE 8)) (define BULB-RADIUS (/ LIGHT-BOX-WIDTH 2)) ;; bulbs pictures (define OFF-RED-BULB (circle BULB-RADIUS "outline" "red")) (define OFF-YELLOW-BULB (circle BULB-RADIUS "outline" "yellow")) (define ON-GREEN-BULB (circle BULB-RADIUS "solid" "green")) ;; box and bulbs positions (define LIGHT-BOX-CENTER-X (/ SCENE-SIZE 2)) (define LIGHT-BOX-CENTER-Y (/ SCENE-SIZE 2)) (define YELLOW-CENTER-X LIGHT-BOX-CENTER-X) (define YELLOW-CENTER-Y LIGHT-BOX-CENTER-Y) (define GREEN-CENTER-X LIGHT-BOX-CENTER-X) (define GREEN-CENTER-Y (+ LIGHT-BOX-CENTER-Y (* 2 BULB-RADIUS))) (define RED-CENTER-X LIGHT-BOX-CENTER-X) (define RED-CENTER-Y (- LIGHT-BOX-CENTER-Y (* 2 BULB-RADIUS))) (define LIGHT-BOX (rectangle LIGHT-BOX-WIDTH LIGHT-BOX-HEIGHT "solid" "black")) ;; a scene with a light box without bulbs (define MT (empty-scene SCENE-SIZE SCENE-SIZE)) (define +LIGHT-BOX (place-image LIGHT-BOX LIGHT-BOX-CENTER-X LIGHT-BOX-CENTER-Y MT)) ;; adding the bulbs to the light box (define +GREEN (place-image ON-GREEN-BULB GREEN-CENTER-X GREEN-CENTER-Y +LIGHT-BOX)) (define +YELLOW (place-image OFF-YELLOW-BULB YELLOW-CENTER-X YELLOW-CENTER-Y +GREEN)) (define +RED (place-image OFF-RED-BULB RED-CENTER-X RED-CENTER-Y +YELLOW)) ;; show +RED