Lab 1, CSC430, Fall 2024
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 14) 52)
Click Run. What do you see?
Now change it to:
(check-equal? (* 4 13) 52)
What do you see now? Why do you think that is?
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-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:
'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 "Show 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/2020-8-1/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.
(Step 4 is not applicable here....)
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.
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
Exercise 15
Exercise 19
Exercise 27
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.
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.
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.
Then, watch this video. I’d really rather it were text, but for now it’s just a video:
https://screencast-o-matic.com/watch/cYf1cMango
Next, you may want to take a look at the TR Notes, specifically the first one (on cond), which has a hint that will be useful to you.
Then, finish updating your existing program to compile and run in the Typed Racket language.
4.4 Structures
Here’s a simple example of defining a union of structures, and matching such a union, using match.
Note that every structure you define in this course should have the #:transparent tag. There’s a note about this in the Hints linked to in the syllabus.
#lang typed/racket ;; example of using 'match' with structures (define-type Meal (U Breakfast Lunch Dinner)) (struct Breakfast ([a : Real]) #:transparent) (struct Lunch ([zz : String]) #:transparent) (struct Dinner ([x : Integer] [y : Integer]) #:transparent) ;; silly example of match (define (my-meal-fn [my-meal : Meal]) : Any (match my-meal [(Breakfast a) (+ a 487)] [(Lunch zz) (string-append zz " for lunch")] [(Dinner x y) (* 1000 (- y x))])) (my-meal-fn (Lunch "banana"))
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.