Contact Info

(for those who care)

Instant Gratification   



Wed, 19 Oct 2011

Programming Language Roundup

I guess it’s on everybody’s mind lately, but here is an excellent overview of “new” languages to come out in the past few years. Definitely a lot more than I thought there were at first.

In this post I will provide a list of fairly new languages (let’s say 5 years with a little flex) that display interesting features and display higher-order thinking in the way that they tend toward an evolution of past learnings in programming language thinking.

[source…]

The one that sticks out to me is Shen which has a few very interesting features.

Taking a look at each point in turn, once you get past the parenthesis, lisp syntax is very cool. If you try to do a good job of following functional / immutable / transormative programming you end up changing the way you write code.

// procedural style
var abc = 6;
double( abc );
triple( abc );
assert( abc == 36 );

// functional style
var abc = 6;
var def = double( abc );
var result = triple( def );
assert( 36 == result );

// "clean" functional style
var result = triple(
                 double( 6 )
             );
assert( 36 == result );

Obviously the above is a contrived example (to be even “cleaner” you would do away with the temporary “result” variable), but you can see that as you get “more functional” you naturally tend to collect parenthesis, no matter the language you are working in.

The “pattern matching” aspect that I really liked from ML is pretty much syntactic sugar for if / else or case / switch but built in to the language.

I find that I’m often writing code like this:

// old style
function foo( a, b ) {
    if ( 1 == a ) {
        alert( 'foo ' + b );
    }
    else if ( 2 == a ) {
        alert( 'bar ' + b );
    }
    else {
        alert( "unknown action: " + a );
    }
}

// new style
function foo( a, b ) {
    var mapping = {
        1: function( b ) { 
              alert( 'foo ' + b );
           },
        2: function( b ) { 
              alert( 'bar ' + b );
           },
    }

    if ( mapping.hasOwnProperty( a ) ) {
        mapping[a]( b );
    }
    else {
        alert( "unknown action: " + a );
    }
}

// "better" new style
function foo( a, b ) {
    var mapping = {
        1: doAlertWithFoo_ref,
        2: doAlertWithBar_ref
    }

    if ( mapping.hasOwnProperty( a ) ) {
        mapping[a]( b );
    }
    else {
        alert( "unknown action: " + a );
    }
}

// "hypothetical" pattern-matching style
function foo( 1, b ) {
    alert( 'foo ' + b );
}
function foo( 2, b ) {
    alert( 'bar ' + b );
}
function foo( a, b ) {
    alert( "unknown action: " + a );
}

Again, an extremely contrived example, the point being that you treat conditionals as mappings instead of bare if/else clauses. Then, the only bugs you can have are “I am missing a mapping” or “My mapping is criss-crossed” or worst “My target action is buggy”.

The pattern-matching aspect of a language makes it easier (supposedly!) to reason about functions. Instead of having to read inside a function to determine how it behaves (for many if/else cases), you can promote that to the top level of function definition (in the above, read function foo( 1, b ) as: when foo is called with 1 as the first parameter).

The coup de grĂ¢ce for me though is the inclusion of a version of Prolog core within the language. In the same way that regexes make string processing a million times easier, prolog makes logic processing a million times easier. And who would have thought… but logic processing turns out to be a useful thing to optimize from time to time.

You’ll have to bear with me on this one because it’s a little harder to explain. But let’s say we have a computer.

 def isComputer( x ):
    return x has memory and x has cpu and x has hard_drive

 def 4gig_o_ram is memory
 def 2gig_o_ram is memory
 def 1gig_o_ram is memory

 def amd_chip is cpu
 def intel is cpu

 def seagate is hard_drive
 def western_digital is hard_drive

 my_computer = 4gig_o_ram and intel and western_digital

 def canRunWindowsVista( x ):
     isComputer( x ) and has 4gig_o_ram

 canRunWindowsVista( my_computer )?
 => true

Again, a really contrived, terrible example with incorrect syntax. But the idea is that you set out the facts and you let the computer (90% of the time) figure out all the boring crap of making sure your arbitrary constraints aren’t violated.

You run into this problem All. The. Time. when working with config files, validating function parameters, determining if a user has permission to perform an action, etc, etc, etc. Regexes are very information dense around a well-defined problem. “Prolog” is a bit more verbose around problems that you define yourself, which is my theory as to why there has never been a “like-a-regex-but-for-prolog” included in any languages.

I can tell you it excites me to see this Shen project try. Definitely worth paying attention to.

00:52 CST | category / entries
permanent link | comments?

Like what you just read? Subscribe to a syndicated feed of my weblog, brought to you by the wonders of RSS.



Thanks for Visiting!