CS5010 Lab

DrScheme [20 mins]

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 colored code.

Hint: DrScheme comes with a help system called Help Desk. Open the Help menu and select Help Desk. This launches a browser with DrScheme's documentation page. Click "How to Design Programs Languages", then click "Beginning Student". That gives you a reference for the Beginning Student language. You can also hit F1 to access any documentation related to the identifier at the position of the cursor.

Experiment with errors. There are three kinds of errors you can make when writing a program in DrScheme.

  1. syntax error
  2. run-time error
  3. logical error
Make at least a couple of errors from each category.

Pick Homework Partners [10 mins]

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.

Teachpacks [40 mins]

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.

Use place-image and empty-scene to create scenes with your image.

Create rectangles with the following dimensions

  1. 2x4
  2. 4x8
  3. 8x16
  4. 16x32
  5. 32x64
  6. 64x128
Design the function rec-sequence that 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-sequence would 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.

  1. Our state of the world represents the radius of a circle:
          ;; WorldState = Number (positive) 
  2. A function that changes the state of the world:
          ;; WorldState -> WorldStete 
          ;; calculates the next state of the world 
          (define (next-world w)
            (add1 w))
  3. A function that creates a picture of a world:
          ;; WorldState -> Scene
          ;; draws the picture of the world with state w
          (define (world-draw w)
             (place-image (circle w "solid" "black") 
                          (empty-scene 200 200)))
  4. We need a final function that initialises the world, the clock and the draw handlers and starts the animation:
          ;; 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.

Subversion [15 mins]

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.


Bonus: Traffic Lights

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 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

    ;; adding the bulbs to the light box
    (define +GREEN

    (define +YELLOW

    (define +RED

    ;; show