Lab 3
1 Crucial DrRacket tips Part 3
DrRacket has a really nice documentation system. You should figure out how to use it.
Start up Firefox. You don’t absolutely have to do this first, but it makes everything else go faster.
Start DrRacket. *IN EITHER WINDOW*, type "map". Without the quotes. Hit the F1 key. Firefox should open, with a bunch of choices.
Click on the word "map" in the first entry. Voila! Documentation!
Wait, you don’t use Firefox? Maybe you should switch browsers, and use one made by a non-profit whose business model doesn’t consist of selling your information (*cough*).
Use this all the time.
2 Exercises
- Go through this tutorial on scope: https://script.google.com/macros/s/AKfycbyurfxNTlQMuPMRMPID4rwN9byvUwQeeaX1G3_B7xeKecrc98UIL7oSWFLtcVKIf_qwOQ/exec?tutorial=scope There are some things you should know about this tutorial:
This is an experimental tutorial, put together by some folks at Brown University,
Currently you cannot save your progress, and
it uses YET ANOTHER variation of racket oh my goodness, and
this tutorial is quite long, it will probably take you 30-40 minutes. It should fit comfortably in a lab period, so that would be a good time to do it.
Despite all of these, it actually teaches you things that you will need on the quizzes and that were heretofore available only in the textbook, so you’ll appreciate these lessons real soon now. Develop the parse000 function, that accepts an s-expression and uses a single match pattern to return true for s-expressions that are a list containing a number, the symbol 'chris, and a symbol (in that order). It should use an other pattern to return #f otherwise.
Note that in Typed Racket, the type Sexp is the one you want to use for s-expressions.
For this and the next few patterns, you may want to refer to the match pattern examples.
Develop the parse001 function, that accepts an s-expression and uses the same match pattern, but returns the symbol in case of success, and #f otherwise. Use a union type for the result.
Develop the parse002 function, that also uses a single pattern and that succeeds for s-expressions that are lists of length three whose second element is a list of real numbers. On success, it should return the list of numbers. On failure, it should return #f.
Note: on this problem you’ll probably run into one of the dark cornerns of TR+match, specifically occurring when you use a predicate pattern inside a ellipsis pattern. See the appropriate section in "Hints on using TR in CSC430" that’s linked to from the course webpage.
Develop the ohno function that accepts a value and returns the symbol 'okay if the input is a number, and uses error to signal an error where the error message includes the given value. Read the documentation for error to see how this works. Oh, well, here’s an example as well:
(error 'my-fun "expected foo, got ~e" 1234)
In order to test your exceptions, you’ll need to use the check-exn form, which requires a few teeny tiny things that you haven’t seen before: regular expressions and thunks.
So, suppose you’re testing a function named p that signals the error "Ouch my foot" when called with the number 13. Here’s how I’d write that test:
(check-exn (regexp (regexp-quote "Ouch my foot")) (lambda () (p 13))) There are about sixteen things that I should tell you about regular expressions and thunks, but this should be enough to get you started.
Define the Arith language described in the textbook in chapter 3. Please feel free to copy code from the texbook.
Develop the evaluation method described in the textbook. Call the function interp. Write your test cases first!
Develop the method num-adds, that accepts an ArithC and returns the number of additions it contains.