Lab 1, CSC430, Winter 2017
1 Google ID, Starting up DrRacket, registering for a handin account
1.1 Google ID
In order to use Captain Teach, I’m going to need to collect Google IDs for each of you. This makes me cringe, a bit; I’m not a huge fan of allowing Google to collect all of the information everywhere. Unfortunately, Captain Teach is currently built around Google IDs. Worst case, you can always create a fresh Google ID for this class....
Anyway, I need a Google ID from you. Enter it on the form listed in the course syllabus.
1.2 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. 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. Note that the "ID number" field is ignored but must be non-blank. Use "0".
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 htdp/bsl (+ 3 4)
Now, click on execute. 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-expect (* 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)
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-expect (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:
'a
'b
'thisisasymbol
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.
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 http://www.ccs.neu.edu/home/matthias/HtDP2e/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.
4.1 Simple data
Exercise 14
Exercise 15
Exercise 27
Make sure to have at least one test case for every function that you write.
When you’re done with these exercises, show them to me for feedback.
4.2 Intervals
(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.
4.3 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, add
(require typed/rackunit)
right at the top.
This imports the rackunit test module. We will be using this import for the remainder of the course.
Next, update your test cases to use the check-equal? form rather than the check-expect form.
Finish updating your existing program to compile and run in the Typed Racket language.
4.4 Structures
Using the struct form, define a piece of compound data to represent desks, which are a kind of office furniture. 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.