Programming Languages Continue to Innovate

April 29, 2009

Computers can't do much. When the processor in your desktop or laptop is doing anything (and most of the time, it isn't) it is running through different combinations of a small number of instructions. The instructions are humdrum, for example, "put this number in slot A", "put this other number in slot B", "add the numbers in slots A and B". Oh yea, and run a billion of these instructions every a second. The amazement of computers is the mind-blowing speed they can do mind-numbing things.

Around the 1950s some genuis computer scientists got tired of writing mind-numbing instructions and invented computer programming languages. With COBOL and others programs became shorter because the programmer could express instructions in a more human-readable format. This trend of languages increasing in expressive power continues today. Let's take a look at some simple examples of this progression in Java and the more modern language Scala.

Our task is simple: given a list of numbers, we want to map the double of each value and print. So, with a list of (1, 2, 3, 4) we want to wind up with (2, 4, 6, 8). Here's some Java code to accomplish just this (lines that start with // are English comments ignored by the program to explain what each step is doing):

Java Example

Java Requires some Heavy Lifting

// Our list of numbers: 1, 2, 3, 4
int[] numbers = new int[]{ 1, 2, 3, 4 };

// Create a variable to hold the doubled numbers
int[] doubled = new int[4];

// Loop through each number in the first list
for(int i = 0; i < numbers.length; i++) {
	// Assign the doubled number
	doubled[i] = numbers[i] * 2;
}

// Finally, print each of the doubled numbers
for(int number: doubled) {
	System.out.println(number);
}

Scala Example

Work at higher levels of abstraction with Scala

Now let's look at some Scala code to accomplish the same thing:

// Define a function that will double a number
def double ( x: Int ) = x * 2

// Map the doubled value of each number to a new list
val doubledValues = List(1,2,3,4) map double

// Finally, print each number on it's own line
doubledValues foreach println

With Scala we can shorten this even further. Down to a single line:

List(1,2,3,4) map (_ * 2) foreach println

The Scala code can almost be read as English. Java programmers may wonder "where are the dots? Shouldn't this look more like List(1,2,3,4).map(_ * 2).foreach(println)?" It could, but one of the neat properties of scala is that it will reliably infer as much about what your code is trying to say as it can. This goes for missing '.'s between operations, ';'s at the ends of lines, parenthesis around single arguments, variable types, etc. The result is that in Scala programmers can express thoughts more succinctly and Scala's compiler will "fill in the unimportant details".

When COBOL, FORTRAN, and LISP gained popularity over 40 years ago the productivity of programmers increased, the barrier of entry for new programmers was lowered, and the programs written became more robust. Being in the software field is just as exciting today as we continue to see these step functions in productivity, power, and expressive capacity between each generation of modern programming languages.

Comments

 sri's avatar
sri

Scala is a hybrid Object-Oriented/Functional Programming language on the JVM. When I heard that Twitter was using Scala, I was curious and started collecting all the sites and articles to learn scala programming. If you are interested check the link below for the big list I have gathered (more than 200 sites) for learning scala programming.
http://markthispage.blogspot.com/2009/06/more-than-100-sites-to-study-scala.html

 Ben Tels's avatar
Ben Tels

I\341\270\277 sorry, but I have to agree with Barry and wekempf here. Sure, programming languages (or, more accurately, programming language RESEARCHERS) continue to innovate -- but this article doesn give an example of it. It does give a good example of another common theme in IT: Have a good idea? Publish and wait 20 years; then watch it getting rehashed.""

 wekempf's avatar
wekempf

Programming Languages Continue to Innovate"

Maybe, but this post doesn't give any indication of this. There's NOTHING even REMOTELY new about map/reduce in languages.

"The Scala code can almost be read as English."

Really?!?!! "List(1,2,3,4) map (_ * 2) foreach println" is about as non-English as I think you can get. Is this understandable to a programmer? It should be, though an imperative programmer may be puzzled by it. But hand this to a non-programmer, English speaking person and ask them what it means."

 Kris Jordan's avatar
Kris Jordan

@Johan - C# and .Net 3.0, in general, have merged functional and object-oriented paradigms as Scala does in a very neat way. I had the opportunity to sit down with Anders Hejlsberg for lunch this past Summer, architect of C#, and hear some of his thoughts on language design, the future of C#, etc. Anders believes we'll continue to see an amalgamation of functional and object-oriented languages. The C# team, and Microsoft's Developer Division, get it (and drive it) in a way they don't get enough credit for.

@Vladimir, @OtengiM - Thanks for the example in Java. Sorry it stripped out your types on the generic list. This is more concise than my example but also demonstrates a place where Java has really fallen behind with Type inference. C# now has type inference. With the generics, you've had to tell Java 4 times you're working with 'int' where Scala would infer it directly. There are much better examples of Scala's design and features dominating comparable Java code (Traits, Object-Pattern Matching, Control Structures Returning Values, etc) but the goal for this blog post was to stay at a high level that non-expert programmers could enjoy.

@Barry - This post does call out an imperative example vs. a functional example. The interesting thing is that we're seeing languages with object-oriented and functional paradigms emerge. Scala isn't the first to do this, but it is one of the most elegantly designed languages I've seen. Rather than trying to be uniquely it's own, Scala feels like a sincere and outstanding attempt to unify the best concepts of many other languages.

 Barry's avatar
Barry

Scala is a functional programming language with roots in Lisp, Scheme, Standard ML, and Haskell, among others. These languages have been capable of succinct expressions like your example for decades.

Haskell: map (* 2) [1..4]

Scheme: (map (lambda (x) (* x 2)) '(1 2 3 4))

Standard ML: map (fn x => 2 * x) [1,2,3,4];

I'm glad to see that FP is finding its way back into the mainstream. What's old is new again!

 OtengiM's avatar
OtengiM

ROFL the last Java one by Vladimir looks better and compact. What is the point to have more new languages just with a few syntatic sugar as Scala?!. I think we need something really new as Haskell or Erlang or a shift of paradigm and not just syntatic sugar to existing languages.

 Vladimir's avatar
Vladimir

Your comment parser kill my generic list.

 Vladimir's avatar
Vladimir

List doubled = new ArrayList();
for(int n : new int[] {1,2,3,4})doubled.add(n * 2);
System.out.println(doubled);

 Johan's avatar
Johan

One way to do it in C# is to use:

Enumerable.Range(1, 4).Select(p => (p * 2)

And with the output (which could probably be done in a shorter fashion)

Console.WriteLine(string.Join(, ", Enumerable.Range(1, 4).Select(p => (p * 2).ToString()).ToArray()));"

Leave a comment