1 Crucial Tips on using Dr Racket, part 1
1.1 Source position information
1.2 Moving by s-expressions
2 Lab 2 exercises

Lab 2

1 Crucial Tips on using DrRacket, part 1

1.1 Source position information

Does a test case of yours cause an error? In this case you’ll get MUCH better information from DrRacket if you lift the offending expression out of the test expression.

Type this code:

  #lang plai
  (define (my-fun a b)
    (+ (a) (b)))
   
  (test (my-fun 3 4) 16)

... and you get back an error like this:

  (exception (my-fun 3 4) "procedure application: expected procedure, given: 3
  (no arguments)" "at line 4")

Unfortunately, the test form is obscuring the helpful information that DrRacket can show you. To see a bunch more helpful information, make a new copy of the expression that caused the error, so now your test code looks (temporarily) like this:

  #lang plai
  (define (my-fun a b)
    (+ (a) (b)))
   
  (my-fun 3 4)
  (test (my-fun 3 4) 16)

... and run it again. Voila! Error location highlighting, and lots of other useful information.

1.2 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 <option>-left and <option>-right, you can move the cursor by a matched set of parentheses or datum. So, for instance, if your cursor is at the beginning of the program, you can use <option>-right twice to move down by two full expressions.

  #lang plai
   
  (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.

Okay. There’s a lot more to show you in this domain, but let’s get on with the PL content for now.

2 Lab 2 exercises

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

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

  3. extend the definition of office furniture to include Bookshelves. A bookshelf has a depth, a number of shelves, and a shelf-width.

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

  5. Define the AE language we described in class.

  6. Develop the evaluation method we described in class.

  7. Develop the method num-nums, that accepts an AE and returns a number indicating how many numbers it contains.

  8. Develop the method num-adds, that accepts an AE and returns the number of additions it contains.

  9. Develop the method swap-adds, that accepts an AE and returns a new AE where the left and right terms of every addition are swapped.