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. In the "ID #" field, put your cal poly id, without the "@calpoly" part.
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.
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?
What else can we represent, besides numbers?
Well, it turns out that we also have booleans:
(or false true)
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.
Okay, on to some exercises from htdp 2e (available online at https://htdp.org/2018-01-06/Book/part_one.html).
(Step 1 is not applicable here...)
Write a one-line purpose statement (comment) describing what the function does, along with the function header.
Write a test case.
Add a template to the function body. It will be the trivial template, for simple data like this.
Finish the function.
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.
When you’re done with these exercises, show them to me for feedback.
(Problem borrowed from HtDP 1st ed.:) Develop the function interest. Like interest-rate, it consumes a deposit amount. Instead of the rate, it produces the actual amount of interest that the money earns in a year. The bank pays a flat 4% for deposits of up to $1,000, a flat 4.5% per year for deposits of up to $5,000, and a flat 5% for deposits of more than $5,000.
Because this function returns a number, you should probably use the check-= function to test it. You can see the documentation for this (and every other function) by hitting F1 while the cursor is on the word check-= in the definitions or interactions window.
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
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.
Using the struct form, define a piece of compound data to represent desks, which are a kind of office furniture. (Take a look at the TR guide to see how structures work, particularly sections 1 and 2.) A desk has a width, a height, and a depth. Provide an example.
Using the match form, develop the furniture-footprint function that consumes a desk and returns its footprint—
that is, how much floor space it takes up. Write test cases first.
Extend the definition of office furniture to include Bookshelves. A bookshelf has a depth, a number of shelves, and a shelf-width. Provide an example
extend test-cases and definition of of the furniture-footprint function so that it handles bookshelves, as well as desks. When you finish this part, show it to me for lab credit.
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.