hoosier ee

wholesome midwestern tech/design weblog

Discomfort Zone

2014-10-05

I was a smart kid. At least, I always thought so, until middle school. I found myself in the "advanced" math class, totally dumbfounded by the concept of negative numbers. This was the first time understanding something in school wasn't easy, and it made an impression. I remember the initial terror and uneasiness, followed by intense brain exercise, the sense of accomplishment and finally the realization that the world is bigger than it was before.

Over the years, as my hobbies and passions evolved, I've sought this experience time and again. In my first class on the C programming language, I got a double-dose of it. First the mild version (`for` and `while` loops), then extra-spicy (pointers).

Later I decided to learn a pure functional programming language (Elm) in the middle of another class assignment. The assignment was to create a software company and launch a product, with a team. Learning a brand-new language in the process elevated the assignment from nigh-impossible to ludicrously crazy, but it was so worth it.

APL-keybd2
My keyboard does not look like this.

Recently, a rather esoteric programming language called J has been calling my name. It's an APL dialect, open-sourced and modern, but doesn't require a special keyboard. From my perspective as a complete beginner, a couple things stand out right away:

  1. "J labs", which are like interactive chapters from a user manual or textbook
  2. amazingly good documentation, which is mandatory because
  3. J is an awful term for a search engine

Some of the surprises are pleasant (% means division, because it looks like an obelus) and some aren't. But for me the most interesting aspect is the focus on tacit programming. This is totally different from most programming languages. A contrived example to show the contrast:

First, in JavaScript:

function addThenSquare(var a, var b) {
 return Math.pow((a + b),2);
}
addThenSquare(10,20); // returns 900

Explicit J version:

 addThenSquareExplicitJ =: verb :'*:+/ y'
 addThenSquareExplicitJ 10
100
 addThenSquareExplicitJ 10 20
900 

J parses right-to-left so it reads as "sum the list `y`, then square the result". Broken down further:

/ NB. insert
+ NB. sum
*: NB. square 

But J provides facilities for tacit functions as well, which can have some advantages.

 addThenSquareTacitJ =: *:@:(+/)
 addThenSquareTacitJ 10
100
 addThenSquareTacitJ 10 20
900 

The extra symbol @: is called at in J, and kinda looks like the dot notation used in mathematics to show function composition. And that's what it does here.

However, there's a lot more to this little phrase. The uninitiated may enter this into an interactive J console session:

 *:+/ 10
100
 *:+/ 10 20
900 

...And fully expect to be able to assign *:+/ to a name like so:

 addThenSquare =: *:+/
 addThenSquare 10 20
900
 addThenSquare 10
10 

Huh? What's going on here? The problem is that the J interpreter can't read your mind. You have to understand how J parses sentences to properly utilize its features. There are these things called hooks and forks which are somewhere between operator precedence and English sentence formation rules.

It's early in the learning curve for me and J at the moment, but so far it's been very rewarding. Lots of neurons shaking the dust off and stretching their axons. I already have a bunch of ideas for projects in J but no clue how to implement them. Stay tuned for more.