1 Crucial tips on using Dr Racket, part 2
2 Actual Brain Exercise

Lab 3

1 Crucial tips on using DrRacket, part 2

In order to avoid wasting all your time counting parentheses, it’s important to take advantage of the tools that DrRacket provides.

First, and fairly obvious, is paren-matching. It’s probably already obvious to you that when you type a close-paren, DrRacket shows you the matching opening paren. When a paren is highlighted in red, it means that it’s not matched properly.

Along similar lines, when you hit <return>, DrRacket will put the cursor in what it believes to be the right place. When it puts the cursor in a place that surprises you, that’s probably a signal that your parens are not closed the way you expected them to be. For instance, suppose you’re trying to develop a function that adds two numbers together, and you type the first line as:

  (define (add-nums a b))

... note the extra closing paren. Then, when you hit return, you’ll notice that the cursor goes all the way back over to the left edge, rather than being indented. That’s a clue to you that your definition was inadvertently closed off.

Next, following up on <option>-left and <option>-right: besides just moving back and forth across s-expressions, you’d like to be able to shuffle them around. DrRacket, like other Windows and Mac OS X applications, allows you to highlight by holding down "Shift" and using the cursor keys. If you use Shift + the s-expression-motion keys, you can cut and paste whole s-expressions, and *never get the parens wrong*. Here’s what I mean: Suppose you have the expression:

  (g h
   ((a b c)
    (d e f)))

And you want to move the expression (d e f) somewhere else in the program. One choice is to use the mouse, and count parens carefully (or patch it up afterward). A better way is to put the cursor on the open-paren, hit Shift-Alt-right arrow, and then just cut using Ctrl-X or whatever you like. This way, you won’t accidentally get too many or too few parens, and the remaining expression will still be balanced.

Try this. Paste the expression in before the (a b c). Then cut the entire ((d e f) (a b c)) expression out, and paste it outside the (g h).

Okay, end of finger exercise.

2 Actual Brain Exercise

  1. Develop a representation for fruits; A fruit can be either a lemon, a melon, or a nomel. Each one has a single field that is a number [*]. Define these using a define-type.

  2. Develop the function onlyLemons, that consumes a list of fruits and returns a list containing only the lemons.

  3. Develop the function onlyMelons, that consumes a list of fruits and returns a list containing only the melons.

  4. Abstract over the two of these to obtain the function onlyThis, that consumes a list of fruits and a particular fruit f (e.g., (lemon 4))and returns a list containing only those elements of the list that are equal? to f. Show me this function when you’re finished with it, for lab credit.

  5. 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].

  6. Develop the my-take function that consumes a list and a number n and returns the first n elements of the list. If the list contains fewer than n elements, it returns the entire list.

  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.

[*] 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*.