The Rise of “Inheritance” In well-thought-out languages, functions can have inner functions, as well as taking other functions as input and return function as output. Here are some examples illustrating the use of such facilities: subroutine generatePower(n) { return subroutine (x) {return x^n}; }
In the above example, the subroutine generatePower returns a function, which takes a argument and raise it to nth power. It can be used like this: print generatePower(2)(5) // prints 25 Example: fixedPoint: subroutine fixedPoint(f,x) { temp=f(x); while (f(x) != temp) { temp=f(temp); } return temp; } In the above example, fixedPoint takes two arguments f and x, where f is taken to be a function. It applies f to x, and apply f to that result, and apply f to that result again, and again, until the result is the same. That is to say, it computes f[f[f[...f[x]...]]]. FixedPoint is a math notion. For example, it can be employeed to implement Newton's Method of finding solutions as well as many problems involving iteration or recursion. FixedPoint may have a optional third parameter of a true/false function fixedPoint(func,arg,predicate) for determining when the nesting should stop. In this form, it is equivalent to the “while loop” in procedural languages. Example: composition: subroutine composition(a,b,c,...) { return subroutine {a(b(...c...))}; } The above example is the math concept of function composition. That is to say, if we apply two functions in sequence as in g[f[x]], then we can think of it as one single function that is a composition of f and g. In math notation, it is often denoted as (g∘f). For example, g[f[x]]→y is the same as (g∘f)[x]→y. In our pseudo-code, the function composition takes any number of arguments, and returns a single function of their composition. When we define a subroutine, for example: subroutine f(n) {return n*n} the function is power of two, but the function is named f. Note here that a function and its name are two different concepts. In well-thought-out languages, defining a function and naming a function are not made inseparable. In such languages, they often have a keyword “lambda” that is used to define functions. Then, one can assign it a name if one so wishes. This separation of concepts made many of the lingustic power in the above examples possible. Example: lambda (n) {return n^2;} \\ a function (lambda (n) {return n^2;})(5) \\ a function applied to 5. f = lambda (n) {return n^2;} \\ a function is defined and named f(5) \\ a function applied to 5. lambda (g) {return lambda {g(f)} } \\ a function composition of (g∘f). The above facilities may seem exotic to industrial programers, but it is in this milieu of linguistic qualities the object oriented paradigm arose, where it employees facilities of inner function (method), assigning function to variable (instantiation), function taking function as inputs (calling method thru object), and application of function to expressions (applying method to data in a class). The data-bundled-with-functions paradigm finds fitting application to some problems. With the advent of such Objet-Oriented practice, certain new ideas emerged. One of great consequence is the idea of inheritance. In OOP practice computations are centered around data as entities of self-contained boxed sets (objects). Thus, frequently one needs slightly different boxed sets than previously defined. Copy and Pasting existing code to define new boxed sets quickly made it unmanageable. (a messy set of classes). With powerful lingustic evironment and habituation, one began to write these new boxed-subroutines (classes) by extending old subroutines (classes) in such a way that the new subroutine contains all variables and subroutines of a base subroutine without any of the old code appearing in the body of the subroutine. Here is a pseudo-code illustration: g = subroutine extend(f) { new variables ... new inner-subroutines ... return a subroutine that also contains all stuff in subroutine f } Here, “extend” is a function that takes another function f, and returns a new function such that this new function contains all the boxed-set things in f, but added its own. This new boxed-set subroutine is given a name g. In OOP parlance, this is the birth of inheritance. Here, g inherited from that of f. f is called the base class or superclass of g. g is the derived class or subclass of f. In functional terms, inheritance mechanism is a function E that takes another function f as input and returns a new function g as output, such that g contained all enclosed members of f with new ones defined in E. In pure OOP languages such as Java, the function E is exhibited as a keyword “extends”. For example, the above code would be in Java: class g extends f { new variables ... new inner-subroutines ... } Here is the same example in Python, where inheritance takes the form of a class definition with a parameter: class g(f): new variables ... new inner-subroutines ... Data are the quintessence in computation. Because in OOP all data are embodied in classes, and wrapping a class to each and every variety of data is unmanageable, inheritance became the central means to manage data. ----- to be continued tomorrow. This is part of an installment of the article “What are OOP's Jargons and Complexities” by Xah Lee, 20050128. The full text is at http://xahlee.org/Periodic_dosage_dir/t2/oop.html © Copyright 2005 by Xah Lee. Verbatim duplication of the complete article for non-profit purposes is granted. The article is published in the following newsgroups: comp.lang.c,comp.lang.c++,comp.lang.lisp,comp.unix.programmer comp.lang.python,comp.lang.perl.misc,comp.lang.scheme,comp.lang.java.programmer comp.lang.functional,comp.object,comp.software-eng,comp.software.patterns Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list