Lab 2
1 Crucial Tips on using Dr  Racket, part 1
1.1 Paren Matching
1.2 Highlighting by holding Shift
1.3 Cutting and Pasting using keystrokes
1.4 Moving by s-expressions
1.5 Highlighting using s-expressions
1.6 Auto-tabbing
1.7 Automatic Parens
2 Lab 2 exercises
3 Actual Brain Exercise
6.2.0.3

Lab 2

1 Crucial Tips on using DrRacket, part 1

1.1 Paren Matching

The first few of these are things that you probably already knew, but I want to make sure.

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

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

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

1.4 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, also known as an "s-expression". 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-typed
 
(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.

1.5 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).

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

1.7 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)))))

2 Lab 2 exercises

3 Actual Brain Exercise

  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

  2. Develop a representation for processors; A processor can be either an Intel, an AMD, or an ARM. Each one has a single field that is a number [*]. Define these using a define-type.

  3. Develop the function onlyIntels, that consumes a list of processors and returns a list containing only the Intels.

  4. Develop the function onlyAMDs, that consumes a list of processors and returns a list containing only the AMDs.

  5. Abstract over the two of these to obtain the function onlyThese, that consumes a list of processors and a particular processor predicate f (e.g., AMD?)and returns a list containing only those elements of the list that satisfy f. (Note: you’re passing the function AMD? here. If this doesn’t make sense, ask for help!

  6. Develop the my-append function that consumes two lists and returns the result of appending the second one to the first. So, if called with the list [a,b,c] and the list [d,e,f], it would return [a,b,c,d,e,f]. NOTE: there is a way to specify a generic list, using the type (listof ’a). Alternatively, you may simply restrict the type to being, say, a (listof number). Either way.

  7. Develop the my-drop function that consumes a list and a number n and returns the list that remains after the first ’n’ elements of the input list. If the length of the input list is <= n, this function returns an empty list. The same comments from the previous problem apply here.

  8. Define the ArithC language described in the textbook in chapter 3. Please feel free to copy code from the texbook.

  9. Develop the evaluation method described in the textbook. Call it eval. Write your test cases first!

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

  11. Develop a parser for the Arith language. It should accept only well-formed s-expressions, and output ArithC’s. It should use the s-exp-match? form. It should signal an error, by calling error, when the input is not well-formed. Here’s a grammar for the language:

      Arith = num
      | {+ Arith Arith}
      | {* Arith Arith}

    ... where id is an arbitrary symbol.

    Write lots of test cases. Use the test/exn form to test your error code.

    This will be the basis for the parser that is a part of your remaining assignments, so it would behoove you to do a good job.

  12. Develop the one-line parse-eval, that accepts an s-expression and calls the parser and then the eval function.

    [*] I don’t care what the field is called. Actually, I don’t care whether they have fields or not, but experience shows that constructors with no arguments along with higher-order procedures confuse the heck out of students just getting used to Racket. If this doesn’t make sense to you, just take my advice and make up a field for each one.