Skip to content

Why borrow when you can use?

December 19, 2012

Let me give you a short exercise. Let’s assume that we are to write a method (a function) that will repeat or double  with whatever is given to it. For example if we pass it a string, the method replicates the string. If it is given a number of some sort, it will double it.

In Java this will be done using method overloading. Something like this is done.

public int mydoubler (int x) {
   return x + x;
}
public double mydoubler (double x) {
   return x + x;
}
public String mydoubler (String x) {
   return x + x;
}

What about in C++? Well you will probably do a function template for this. Like

template <class T>

T mydoubler (T x) {

  T result;

  result = x + x;

  return (result);

}

Then in the main you will have something like this…

 double I = mydoubler<double>(1.0);

Now see how we can do this in Scheme/Lisp (inspired from Dr. Racket Guide)

(define (mydouble x)

  ((if (string? x) string-append +) x x))

Notice how concise this is at least compared to Java.  The advantabe to C++ in terms of brevity is just slightly better but when I call this function in Scheme/Lisp I simply do this

(mydouble 2)

(mydouble “ha”) 

etc etc

I do not have to declare anything like defining I  as shown in the above  C++ code.

So what is the moral of this story?

Please do not get me wrong,  I do not mean to be rude. I owe a lot from C++ and Java, I earned a good living for more than 12 years on them and I am still paid for Java programming today on a casual basis.

My point is that most of the stuff we see in modern programming languages have been borrowed from the old one – LISP. For example, javadoc as a concept has already been present in LISP for many years, that is where Java got the idea from. Then we see new languages today like Ruby borrowing concepts  from the old one.  So the question is, if you are going to invent a new programming language and borrow from the old one, what the heck was wrong with the old one since you keep on borrowing from it and embedding its features into your new language?

According  to this article, functional programming is on the rise. I hope that becomes true because it offers a lot more excitement, elegance and creativity to programmers. That is where I am right now.

No comments yet

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.