Friday, September 10, 2010

Daily, 2010/09/10: Procedures as Values, Things, and Other Stuff

Announcement items:
  • Old announcement items: see blog 
  • Web redirect fixed (http://www.ugrad.cs.ubc.ca/~cs311 should work) 
  • Tutorial registration issue 
  • Your TODO items: see notes page of website 
  • Tutorial collab policy update + minor add'l update
High-level comment:

We've finished what I really wanted to show in Racket: programming lessons on the basics you'll need and some emphasis on the extraordinary power of "first-class functions" (functions that can be used as normal values).  We'll discuss two remaining Racket constructions (define-type and type-case) as we explore the AE mini-language on Monday.

However, I was disoriented from running to and from my office before class and may have been occasionally incoherent, particularly answering your questions and comments!  I'll avoid hyperventilating before class in the future.  Help me out as long as I have this ear infection by making your contributions loud!

Details:


Today was another whirlwind: refresher + introduction to identifier binding, function definition, anonymous functions (lambdas), cond expressions, local expressions, and use of functions as values.

We lingered on the tremendous power of passing functions as values, as when we quickly constructed only-pos from filter and positive? (hat tip to Paul (?) who pointed out that predefined procedure). That's time well spent, since our first string of interpreters will build toward fully expressing this powerful function construct.

We spent less time on another profound point: Where does the lambda in the procedure below find x's value, and how does it hold on to that binding of x to its value? We'll have to solve those problems before we can take advantage of functions-as-values. Programmers in languages (like Java and, until recently, Python) that do not solve that problem cannot easily exploit this power:

(define (sort-closest-to x lon)
  (sort (lambda (y z)
          (< (abs (- x y))
             (abs (- x z))))
        lon))

We'll have far more to say about this in the near future!

(What do I mean by "how does it hold on to that binding"? A natural way to model execution of this lambda is to envision that when it's called, execution "jumps" to the point in the code where the lambda is defined, and x is just available there. That's a reasonable mental model, and we humans are comfortable moving our fingers across the page of code and saying "execution continues here". However, it leaves open the question of what machinery programs need to emulate that model, and just which binding of x to a particular value is actually available!)

P.S. One Racket form you'll probably use that we haven't touched is the case expression, which is much like a Java switch:
(case expr
  ['foo (do-something-when-expr-evals-to-the-symbol-foo)]
  ['bar (do-something-when-expr-evals-to-the-symbol-bar)]
  ...
  [else (do-something-otherwise)])

Cheers,

Steve

No comments:

Post a Comment