Re: Python had a little lambda ...

Bennett Todd (bet@std.sbi.com)
Wed, 23 Feb 1994 22:53:53 -0500 (EST)

Thanks for your kind reply. I guess I didn't make myself clear. I wrote:

>The one thing I've found that TCL is __REALLY__ good at, that no other
>language I've found matches, is extensions that have lots of callbacks.
>TCL's syntax exposes the view that an extension like expect, or the widgets
>of Tk, are nonlinear flow-of-control constructs.

That's really what I'm hoping to get at, here. In TCL, a function like
expect, that takes many arguments, many of which are callbacks, can be coded
like a switch statement. It is as clean to use as the builtin
flow-of-control constructs like if and while. This works in TCL because
curly braces are just string quotes, and a code block is just a string,
passed in as an argument.

By contrast, in Perl, &chat'expect is relatively ugly to use. You have to
pass in strings containing the callbacks, which are then eval-ed. The
string quoting rules don't end up being really pleasant for code.

If there were a nice multi-line lambda, then we could write an expect
function which could be invoked something like:

res = expect(timeout,
'regexp1', lambda (match1):
code to execute when you see a regexp1 pattern
more code for handling regexp1
'regexp2', lambda (match2):
code to execute when you see a regexp2 pattern
...
)

Likewise, a Tk-like widget set might have a function something like:

newbutton = button.create
newbutton.action = lambda (event):
what to do when someone presses newbutton

Sure, whenever you have a callback, you can create a dummy function,
inventing a name for it, then pass the function in. But in some kinds of
programming, which are just filthy with callbacks, it reads better to
``pretend'' that the function that takes the callbacks is a new
flow-of-control construct. This is where TCL currently stands alone. It's
not yet clear to me that Python can't outdo all the other modern scripting
languages in their own strengths, but I don't yet see a way to create a
python module that is as clear and beautiful as Don Libes' expect(1). And
the beauty of Tk scripts reveals that the concept has other uses.

-Bennett
bet@sbi.com