[Tfug] Language wars ( was Language choices )
Tim Ottinger
tottinge at gmail.com
Thu Nov 2 19:00:43 MST 2006
On 11/2/06, Robert Hunter <hunter at tfug.org> wrote:
>
> Tim, some interesting points, but just want to clear up a few things...
>
> > "Anonymous function" is too long to type, of course. Closure sounds
> > best. And reminds me of ladies foundation garments, which is a
> > pleasant association.
>
> An anonymous function is not necessarily a closure. A closure is
> function which in some way "captures" the state of it's environment, or
> in computer science lingo, *refers to the free variables in its lexical
> context. Anonymous functions are just functions that are not bound to a
> name. Perhaps in some languages construction of a closure requires the
> use of an anonymous function, but technically they are not the same.
Ah, not being a Java guy, I assumed there was closure in the anonymous
function. In nested functions in python, you get closure. Also in lambdas
these
days (not always so, but they got better). Ah, well. Java. I guess you take
as
much as you get.
> If we had more high-level functions (functions taking function params)
> > we would begin to realize that we don't need comps and generators
> > nearly as much as we think we do.
>
> Do you mean "higher-order" functions? And what is a "comp"?
Yes.
Comp = list comprehension, a Python thing that's been very popular.
mylist = [ item for item in yourList if item.age > 30 ]
> Actually, having blocks greatly reduces the number of things you need
> > the language to do for you, including "if" and "case".
>
> Can you give an example?
If boolean were a real class, and had an if true method you could pass a
block to:
x > 9 ifTrue: [someCode] ifFalse: [otherCode].
There. No need for "If" "else" in the language -- it's in the library.
Send "ifTrue" to a boolean.
Just another message going to just another object.
> It amazes me how much language syntax the closure mechanism obviates.
> > Switch/case is just a dictionary with some value-based key and a
> > closure for a value. Iterating a tree is just a matter of passing a
> > closure to a general function that iterates.
>
> Once again, I think you are confusing a closure with a function
> reference. Notice that some of these things you mentioned can be
> accomplished in C
Yes, you can, but it's unnatural and kinda sucks. :-)
A closure is more convenient and powerful, obviously.
, a language in which you can't construct closures. For
> example, consider the qsort function in C. The function pointed to by
> "compar" defines the actual order that is used by the underlying sorting
> algorithm. It is not a closure.
>
> void
> qsort( void *base,
> size_t nmemb, size_t size,
> int(*compar)(const void *, const void *) );
No, but it's not cool like a block:
array sort: { |x,y| x.name < right.name }
someList.sort(cmp= lambda x,y: x>y)
That's a lot less. effort, and I did it twice. In two languages.
By the way, speaking of other languages in which you *can* use closures,
> don't forget about Scheme and Perl ;-)
Right. You should throw a few examples my way. I would like to see the
Scheme.
I could skip the perl.
More information about the tfug
mailing list