Wednesday, September 15, 2010

Daily, 2010/09/15: multi+ and with

Announcement items:
  • Thoughts on Reading, Lectures, and Assignments
  • A#1 due FRIDAY; it would help A LOT to read Chapters 1, 2, and 3
  • Thoughts from interactions with students over A#1 (notes to be posted below)
High-level comment:

Today was practice with Racket and parsers: no profound message!  (Well, except the request to think hard about whether (interp (parse '{with {x {+ 1 2}} 2})) should ever cause the interpreter to add the numbers 1 and 2.)  Thus, the details section just contains the notes promised above.

I did try to model how to build a function based on the types the function operates on (e.g., breaking down our parser into cases for each of the types of AE we'll generate).  This is useful, especially to answer (or avoid ever asking) questions like "Why did (second sexp) cause an error here?  Oh, because (second sexp) is not an AE, but I have a function that consumes sexps and produces AEs: I can recursively call parse!"  See How to Design Programs for an extensive introduction to programming using templates like this one (but better).

One other critical thing that happened: a student answered one of my polls by saying he didn't know why we were asking the poll, the single most useful answer.  I'll post more about this on Vista.


Details:


Promised in the announcements:


#1: (test ...) statements are critical to help you develop, but when you see an error, use the interactions pane (at the bottom) to investigate.  So, if you have a test like:

(test (parse '{+ 1 2}) (binop + (num 1) (num 2)))

that's not working..  At the prompt (the little "> " thing), try:

> (parse '{+ 1 2})

Then, when you find the broken code, try executing the parts that matter directly, e.g., maybe your (incorrect) code looks like:

  (binop (first sexp) (second sexp) (third sexp))

Then, try this.  Add a define into your file (not the prompt, unless you want to use "local") that sets up the sexp you're interested in:

  (define testing-sexp '{+ 1 2})

Rerun, and at the prompt say:

> (binop (first testing-sexp) (second testing-sexp) (third testing-sexp))

Then, play around with the errors and the little code snippet until you gain insight and can work back in your file at the top!



#2: define-type is essentially a shorthand for writing LOTS AND LOTS of handy functions with define.  Among the functions created by a define-type is one for each variant (like with, with*, and binop in our assignment) that takes one argument for each field of the variant and creates the corresponding argument.  These are just functions.  How do you call them?  Like functions:

> (binop + (num 1) (num 2))

The first thing in the list is a function: binop for the larger list, num for each of the two smaller lists.  Everything else is evaluated to get a value.  (So, binop => the binop creation procedure.  + => the procedure that adds.  (num 1) => [first num => the num creation procedure and 1 => the number 1; then, the num creation procedure is applied to the number 1 to make a num object].  Ditto for (num 2).

Cheers,

Steve

No comments:

Post a Comment