Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Lambda abstractions in C++ vs. Scheme

Name: Anonymous 2009-07-03 12:54

In Scheme:
     (define foo (lambda (a b) (+ a b)))
The lambda-expression when evaluated produces the value of a procedural type. The define form binds this value to an identifier foo. Evaluation of an expression
     (foo 1 2)
looks up the procedural value bound to this identifier, and applies it.

In C++, precisely the same idea is expressed as

     Lambda((int a, int b), int, return a+b) foo;
where

     #define Lambda(args,ret_type,body) \
     class MakeName(__Lambda___) { \
     public: ret_type operator() args { body; } }
The Lambda construction creates a procedural type (class). The instantiation operator -- called 'declaration' in plain C -- "binds" this apply-able type to an instance foo. This sounds better in reverse: foo is bound to a value of a procedural type. Or, foo is instantiated as an object of a class that implements a callable interface.

When the compiler processes an application:
     cout << "foo(1,2) is " << foo(1,2) << endl;
it looks up the type of foo, finds the procedural class and invokes the appropriate method. The similarity between the two expressions -- in Scheme and C++ -- is almost complete. The major difference is a compile vs. run-time lookup of values and bindings. Optimizing Scheme compilers blur even this difference.

Name: Anonymous 2009-07-03 14:44

The venerable master Sussman was walking with his student, Anonymous.  Hoping to
prompt the master into a discussion, Anonymous said "Master, I have heard that
objects are a very good thing - is this true?"  Sussman looked pityingly at
his student and replied, "Foolish pupil - objects are merely a poor man's
closures."

  Chastised, Anonymous took his leave from his master and returned to his cell,
intent on studying closures.  He carefully read the entire "Lambda: The
Ultimate..." series of papers and its cousins, and implemented a small
Scheme interpreter with a closure-based object system.  He learned much, and
looked forward to informing his master of his progress.

  On his next walk with Sussman, Anonymous attempted to impress his master by
saying "Master, I have diligently studied the matter, and now understand
that objects are truly a poor man's closures."  Sussman responded by hitting
Anonymous with his stick, saying "When will you learn? Closures are a poor man's
object."  At that moment, Anonymous became enlightened.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List