Lab 1, CSC430, Fall 2019
1 Google ID, Starting up Dr  Racket, registering for a handin account
1.1 Dr  Racket
2 Using Dr  Racket
3 Elements of Racket
4 Exercises
4.1 Simple data
4.2 Intervals
4.3 Typed Racket
4.4 Structures
7.5.0.16

Lab 1, CSC430, Fall 2019

1 Google ID, Starting up DrRacket, registering for a handin account

1.1 DrRacket

Start up DrRacket; it should be installed on the Lab machines. See the course syllabus for instructions on finding it.

After starting up, you’ll need to choose a language. For now, choose "The Racket Language".

Next you’ll need to install the handin plugin. Follow the instructions on the course syllabus to install it.

After restarting DrRacket, you must register a handin account with the handin server. choose the Manage CPE430 User Account... menu item from the leftmost menu. Follow the dialog to create a new user account. As the username, use your cal poly id (without the @calpoly part). In the "ID #" field, put a zero.

Next, we need to check that handin is working properly. Type some text in the topmost window. This is called the "definitions" window. Click on the handin button, and make sure that you can hand in successfully. Note that you may always submit as many times as you like; later submissions overwrite earlier ones, so you don’t need to worry about submitting an early incomplete solution. However, you must be careful that your final submission contains all of your work.

Now, mess up the text in the definitions. Then, using the Handin button again, retrieve the text you submitted earlier. This should now be different from your changed text.

2 Using DrRacket

Next, erase the existing junk and type a short legal Racket program in the definitions window. How about this one:

#lang racket
 
(require rackunit)
 
(+ 3 4)

Now, click on Run. The lower window (called the "interactions" window) should show success.

In the interactions window, we can also evaluate expressions. Type (+ 4 5) and hit return.

Next, we need to see how tests work.

Now add this test:

(check-equal? (* 4 13) 52)

Click Run. What do you see?

3 Elements of Racket

What else can we represent, besides numbers?

Well, it turns out that we also have booleans:

  • true

  • false

  • (or false true)

These are all expressions. Make sure they evaluate to the expected results.

Wait, what? What the heck are those? Figure out what those are. Can you use them in the expressions above?

Write a test case for the third one. That is, something like:

(check-equal? (or false true) <expected result here>)

As you might guess, operations on booleans include and, or, and not.

Also, Racket has symbols. Symbols are something like a cross between strings and enums. symbols can be written like this:

They start with a single quote, and then a sequence of alphanumeric characters. For now, the only operator we’ll use on symbols is equality comparison, with symbol=? .

We can also define functions. Here’s a simple function that adds two numbers together:

; add two numbers together
(define (add-nums a b)
  (+ a b))

Try running this program. Does it work? Who knows? We can’t tell, because we didn’t test it.

One way to see this is to enable "Syntactic Test Suite Coverage" in the "Language Details" section of the Language|Choose Language... menu item. Try turning this on. Run the program again, and see that the body of the function is highlighted in black and orange.

NB: this will save you HEAPS of time in the rest of the course, because the handin engine won’t accept code with un-tested regions.

In order to build a test case, you’ll need to know how to call the function that you defined. It turns out that you call your functions in exactly the same way that you call the built-in functions: an open paren, the name of the function that you’re calling, zero or more arguments separated by spaces, and finally a closing paren.

Try calling the add-nums function, then add a test case for it and run it, making sure that the test case passes. Check that the syntactic test suite coverage tool now shows the body of the add-nums function as covered.

4 Exercises

Okay, on to some exercises from htdp 2e (available online at https://htdp.org/2018-01-06/Book/part_one.html).

For each of the three exercises listed below, You need to follow the design recipe:
  1. (Step 1 is not applicable here...)

  2. Write a one-line purpose statement (comment) describing what the function does, along with the function header.

  3. Write a test case.

  4. Add a template to the function body. It will be the trivial template, for simple data like this.

  5. Finish the function.

  6. Test and repeat as necessary.

Make sure to have at least one test case for every function that you write. Note that the book uses a check-expect form, rather than the check-equal? form that we’re using.

Also, in order to use the string-ith function, ordinarily only defined in the beginner language, we must import it specifically:

(require (only-in lang/htdp-beginner string-ith))

Finally, note that exercise 27 refers to "all definitions in DrRacket’s definitions area"; this refers to the functions mentioned in the text: attendees, revenue, etc.

4.1 Simple data

When you’re done with these exercises, show them to me for feedback.

4.2 Intervals

4.3 Typed Racket

Now, change the language declaration to refer to typed/racket.

You’ll have to make a number of changes in order to make this work.

First, in the definitions window, replace (require rackunit) with

(require typed/rackunit)

right at the top.

This imports the typed version of the rackunit test module. We will be using this import for the remainder of the course.

Next, comment out exercise 14 and its accompanying require. Don’t worry about this problem.

Then, finish updating your existing program to compile and run in the Typed Racket language.

4.4 Structures

When you’re done with these, demo them to me as well. I will give you a code that you can enter into the lab code entry servlet (link on the main course page) to get credit for the lab.