Lab 2
1 Structures
2 Crucial Tips on using Dr  Racket, part 1
2.1 Help!
2.2 Highlighting by holding Shift
2.3 Cutting and Pasting using keystrokes
2.4 Paren Matching
2.5 Moving by s-expressions
2.6 Highlighting using s-expressions
2.7 Auto-tabbing
2.8 Automatic Parens
3 Selecting with the Mouse
4 Programming Exercises
9.3

Lab 2🔗

back to course homepage

1 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.

2 Crucial Tips on using DrRacket, part 1🔗

2.1 Help!🔗

DrRacket has built-in documentation on every function you’ll be using. To access it, put the cursor on an identifier and hit F1. Or choose Help|Help Desk. This documentation is local, and should work fine even without network.

You can also find this documentation online at https://docs.racket-lang.org/, though that source won’t include all of your locally-installed documentation, and it’s obviously not available when you don’t have network.

2.2 Highlighting by holding Shift🔗

When you hold down shift while pressing cursor movement keys, the intervening characters are selected. Try putting the cursor somewhere in your code, and holding shift-down.

I know, you’re not suprised. That’s okay. Unless you were. That’s okay, too!

2.3 Cutting and Pasting using keystrokes🔗

You can use cmd-x or ctrl-x (by platform) and cmd-v or ctrl-v (by platform) to copy and paste. Don’t use the mouse.

Yep, we’re still in Notepad territory.

2.4 Paren Matching🔗

When the cursor is outside of a parenthesis, the matching pair of parentheses are highlighted, along with everything inside. You knew that already, right?

2.5 Moving by s-expressions🔗

You may have noticed that there are a lot of parentheses in Racket. It turns out that this can be a blessing, as well as a curse. By using <special_key>-left and <special_key>-right, you can move the cursor by a matched set of parentheses or datum, also known as an "s-expression". So, for instance, if your cursor is at the beginning of the program, you can use <special_key>-right twice to move down by two full expressions.

On Windows, the special key is alt, and on MacOS the special key is option.

#lang racket
 
(define (f x)
  (* (- (* x x)) (+ x 9) 4))
 
(define (g x)
  (* x x 5))

Use <option>-left and <option>-right to move back and forth through the top-level expressions.

Then, use <option>-left and <option>-right to move back and forth through the arguments to the first * in the definition of x.

The <option>-up combination is also extremely useful; it moves the cursor to the outside of the enclosing expression. Using <option>-up rather than the simple arrow keys means that you’re always in a position to highlight or cut the given s-expression. Also, popping to the outside of a long function with two or three <option>-up’s is much faster than hitting the up key a whole bunch of times.

2.6 Highlighting using s-expressions🔗

You can combine the last two subsections; by holding <shift> while using <option>-left and <option>-right, you can highlight one or more s-expressions. This is most useful when you’re planning to cut them, or replace them (or wrap them in parens... but we’re not there yet).

2.7 Auto-tabbing🔗

Like any other sane editor, DrRacket can indent code for you. When a block of code is highlighted, you can hit <tab> to reindent that block.

Okay, time for a simple exercise.

Here’s a piece of code.

(define (show-example b)
           (begin
 (printf "magnitude of sum of elements: ~s\n"
                  (array-map magnitude (array-axis-fold (my-fft b) 0 + 0)))
 
(printf "sum of magnitude of elements: ~s\n"
        (array-axis-fold (array-map magnitude (my-fft b)) 0 + 0))
 
                 (plot (points (in-array (array->plottable (my-fft b))))
#:y-max 1500
#:x-max 1024
  #:height 300)))

Use the s-expression highlighting keys to highlight just the first printf. Hit <tab>. Note that the printf is now aligned relative to the line before it, but nothing else has changed.

Now, use the s-expression-highlighting keys to highlight the whole function definition. (Not the mouse! Please!) Hit <tab>. See that the whole thing gets re-indented all at once.

Sometimes, you’ll notice that things don’t go where you expect, when you re-indent. This is usually a sign that you have an extra or missing parenthesis, somewhere.

Next simple exercise:

Use the s-expression highlighting expressions to highlight the first printf. Use cmd-x and cmd-v to cut it and paste it outside of the show-example function.

2.8 Automatic Parens🔗

Okay, here’s where the magic gets better. In the Preferences > Editing > General box, check the "Enable Automatic Parentheses" option.

Now, something strange happens; when you type a left-paren ’(’, you’ll see a matching pair of parentheses appear, with the cursor in the middle.

It’s nice not to have to type the closing parenthesis, but it’s even nicer that when you use this binding, your parentheses are never unbalanced.

But wait! What if you want to wrap parentheses around a set of existing expressions? No problem. Just highlight them, using the keystrokes we’ve already described, and then type the left paren. You’ll see a pair of parentheses, wrapped around the highlighted expressions.

The same keystrokes also work for the square bracket, ’[’.

There’s even more in this space, but I think we’d better stop here for a bit.

Here’s a simple exercise; you should be able to complete this without having unbalanced parentheses.

In the following code, change the "if" into a cond. First, change the keyword "if" into "cond". Then, highlight the two following expressions, and wrap them with square brackets. Then, highlight the third one, beginning "append", and wrap it with square brackets, and insert the word "else" following the open bracket. Done!

(define (srl:map-append func lst)
  (if (null? lst)
      lst
      (append (func (car lst))
              (srl:map-append func (cdr lst)))))

3 Selecting with the Mouse🔗

Yes, you can select text with the mouse.

Don’t do it.

You’ll miss parens, and wind up scrambling to fix them. If it takes you a minute to find and fix a paren-matching error caused by a mouse selection, and you do this twenty times an hour, you’ve now wasted a third of your development time. Remember, my goal is to get you out of here and doing other things as quickly as possible.

4 Programming Exercises🔗

These exercises are focused on Lists. In this class, the term "List" will be synonymous with "Linked List", or, if you prefer, "Singly Linked List". The best introduction to lists is probably Sections 8, 9, and 10 of How to Design Programs. Read them carefully; they introduces lists carefully, and goes over the design recipe for functions on lists. There are many short-cuts and elaborations on lists that we’ll be using, but this text presents the bedrock truth of linked lists in a way that’s hard to beat.

  1. Develop the function rev-str-app, that accepts a list of strings and returns a single string combining the input strings in reverse order. Use string-append to join strings. So, calling the function with the list containing the strings "ball", "juice", and "frog" would produce the string "frogjuiceball". Follow the design recipe, including types, purpose statement, and test cases.

    For this problem only, make sure to leave a complete commented-out copy of the list template in the body of the function. Comment it out using "hash-semicolon".

  2. Figure out how to use :print-type to print the types associated with values. What type does rev-str-app have? Does this make sense? What about the type of ’+’? Why is it so long? Write your answers in the form of a comment.