Fundamentals of Computer Science I

CS 2500, Spring 2014

  • Homepage
  • General
  • Texts
  • Syllabus
  • Assignments
  • Labs
  • Blog
  • Advice



Lab 1 - BSL in DrRacket

Purpose The purpose of this lab is to give you some hands-on experience with the BSL programming language and with the DrRacket programming environment for BSL. You will also learn how to submit assignments starting with Problem Set 2.

BSL (Beginning Student Language) is a programming language that you'll start working with (we'll use more advanced languages as the course progresses). DrRacket is an Interactive Development Environment (IDE) where you will develop your programs.

DrRacket comes with three important tools:
  1. the Definitions area: where you define functions, tests, and expressions that need to be evaluated. When you click Save, everything you've written in your definitions area is saved in a .rkt file.

  2. The Interactions area: where you experiment with ideas by writing expressions that DrRacket can evaluate. This area is like scrap paper, nothing your write here is saved when you click save.

  3. The Stepper: a tool that helps you step through the evaluation of function definitions and expressions from the Definitions area.

When you walk out of the lab, you must feel comfortable using all three tools. Computer scientists tend to speak of the toolchain or occasionally the tool box, and proficient programmers must know their toolchain well.

image

Welcome to Lab

Collect contracts from all students who enter the room. Hand contracts to co-pilot TA who matches up pairs on a random basis.

How Pairs Work

You will be working in pairs during labs. Pairs consist of a pilot and a co-pilot. The pilot types at the keyboard while the co-pilot looks over the pilot’s shoulder. Like in real airplanes, pilots and co-pilots switch back and forth during the lab.

Launching DrRacket

Launch DrRacket. It is an interactive development environment aka an IDE. You can use it for many different programming language: Algol 60, Beginning Student Language aka BSL, Datalog, Racket, R6RS Scheme, and a few more. Choose the Beginning Student Language from the {Language | Teaching Languages} drop-down menu.

Interacting with DrRacket

(45 min) Use DrRacket’s interactions area as a calculator. Show a range of operations on numbers, booleans, strings. Use big numbers, use complex numbers. In the future, you don’t need a calculator; use the interactions area. It is your time to play. Re-arrange room so partners get to work with each other. Co-pilot TA must have finished pair assignment now.

Use DrRacket’s definitions area to write down expressions. Nothing happens. Show run. Show the stepper. Time to play again.

Define the following function in the definitions area:

Sample Problem The function how-far consumes the number of minutes you drive at 55mph and produces how far you get in the given time.

Run. Interact. Add an application of how-far to the definitions area. Use the stepper.

Define a function that accepts a number of minutes and computes how many whole hours these minutes represent.

You’ve Got Errors

When you program, you encounter three kinds of errors:
  • syntax errors: these are errors that are the programming equivalent of "bad grammar", meaning that what you wrote is not a valid BSL “sentence” DrRacket will show you these errors in red text.

  • run-time errors: these are errors that occur when you write code that is grammatically correct but stops your program from finishing its execution. These errors include things like trying to divide by zero or if you give a function too many arguments or too few arguments.

  • logical errors: these are errors that occur when you write code that is grammatically correct and will execute, but doesn't perform the task that you intended it to perform. These errors do not trigger any error messages and thus can be more difficult to catch.

Getting an error message is good. All novice programmers make mistakes and you should learn how to interpret error messages and solve the issues.

Demonstrate all three of these errors and explain that testing—to be explained in the future—is a mechanism to defend yourself against logical errors.

Switch partners

Conditions

Define a conditional function.

Sample Problem The function how-hot consumes a temperature (number) and produces one of three strings: "cold" for temperatures below 45 (inclusive), "comfortable" for temperatures between 45 (exclusive) and 75 (inclusive), and "hot" for temperatures above 75.

Explain cond carefully. Explain brackets as an alternative for parentheses that visually set apart cond clauses. Interact. Use the stepper.

It is your turn. (15 min)

Sample Problem Define the function letter-grade. It consumes a score (number) between 0 and 100 and produces a letter grade ("A", "B", "C", "D", or "F"). You may choose your own scale.

Programming with Images

(30 min) Use (require 2htdp/image). Show circles, squares, text. Show “addition” of images.

Find an image of a car on the web. Copy and paste it into DrRacket’s definitions area and give it a name. Determine its width, height, area. Compose a car image with squares, circles, triangles. Draw a red frame around it.

Program a function that processes images.

Sample Problem Define the function frame, which consumes an image and draws a red frame around it.

Interact. Use the stepper.

Switch partners; 15 min

Draw some pictures yourself

Sample Problem Define the function sale. It accepts a wish in the form of a string and, if it recognizes the string, it returns an image of this object; otherwise it produces "sorry" as a large text.

Time to solve one last problem Switch partners

Sample Problem Define a program that consumes a string and judges how long it is. If the string is shorter than 3 characters, we call it "stubby". If it is a bit longer, say up to 10, it’s dubbed a "shorty". For strings, up to 25 characters, we go with "middling". And for a string that’s even longer, the function says it is "too wordy".