Feeds:
Posts
Comments

Archive for February, 2006

Think about responding to non-existence functions, so that the program will not crash on poor use of library. Think about building object on-the-fly based on the schemas of the database, so that you could dynamically wrap the database. When compile-time and runtime configuration (and self-configuration) allows program to adapt to the environment by properly enabling and disabling pre-built functions built at coding time, programs built with dynamic languages can adapt to the environment by self programming at runtime, based on some higher level description given by the programmers at coding time.

Think about evolution. God created men, but he does not have to create every single man. He created men with reproduction capability. I believe this higher-level creation is much more powerful and much less labor. With dynamic languages, you could do higher level programming and create higher level functions which further create more functions. If in your higher level programming you ask every function to inherit the capability of reproduction, then there is this possibility of self-reproduction and evolution. Genetic algorithm can be directly mapped in a much more intuitive way.

Read Full Post »

According to wikipedia, “closure is a function that refers to free variables in its lexical context”.

Sounds pretty vague to me at first. My definition would be, “closure is a nested function, that besides referring to its argument variables and its local and global variables, uses some other variables which are local in the nesting function.” A simple example in Python would be:

def A(j):
   int i = 8;
   def B():
        p = 9;
        return i*j + 9;
   return B;

Here the function B uses variables i and j, both of which are in A‘s scope. When we call A, an instance of function as described in B‘s definition will be created. This newly-created function is a closure, in the form of a function with bound variables. The key idea is to use functions as the first-class citizens, so that they can be created on the fly, passed in as arguments of function calls, and returned as result of function calls.

From the perspective of programmers, closure is used to create dynamic functions at runtime, while in traditional languages, variables and objects are created dynamically at runtime, but functions are static. Languages with closure support can do abstract on control (in the form of subroutine, procedure or function), and are therefore much more expressive. For example, a generic_sort function may take a cmp function as an argument and return a specific sorter based on this customized compare subroutine cmp and the sorting logic encapsulated in generic_sort. The sweet feature of Decorator in Python is another good example of the power of using functions as the first-class citizens.

If you read flamewars on slashdot, you would know that every new hot languages, including Python, Ruby, and JavaScript, now come with features like functions as first-class citizens, closure, and list comprehension. It is really the Revenge of the Nerds, considering that these features were once exclusively used in once-considered-dead and nerds-backed functional programming languages. Scheme implements OOP based on closure in the sense that every object is basically a function (used to respond to messages) bound with some variables storing the state of the object.

I am lucky to learn Scheme before touching any of the new cool dynamic languages. Before touching scheme, I only use the languages that is very industrial (and therefore static, verbose and annoying), such as C and Java, and very dirty (and easy and fast), such as Bash PHP and Perl. Believe or not, serious learning of a functional programming language will refresh your understanding of programming, if you like me have never touched it before. Without thinking in functional programming, programming Python is like you play with Java without wrapping your mind around OOP.

Read Full Post »