Tuesday, December 18, 2012

Happy Happy Joy Joy.. Learning a new programming language

A year ago I started a new job. The company that is now my employer has a lot of code written in an old and obscure language called APL. So I have had to sit down and learn APL.

APL is short for "A Programming Language" and being invented in 1953 it is a very old language. For me its a fresh new approach to programming. Instead of spawning from electronic engineers, APL comes from the twisted mind of a mathematician. That means that the language does not origin from abstracting processor instructions, but from what would be useful for doing math. This makes it an extremely high level language that is very powerful once you wrap you head around it. APL sees the world as vectors and matrixes (single dimensionned arrays and multi dimensioned arrays for the rest of us). Once you have your world defined in arrays, you will be amazed by the expressiveness that APL provides.

I recently listened to hanselminutes where they talked about F#. One of the key points with F# was how little code you had to write to accomplish anything.  The example given was Conway's game of life. Ruby could do it in 140 characters. F# weighed in with an impressive 127 characters. Compared to APL these languages are however very verbose. APL does the same thing in only 38 characters. To be fair, this problem is right down APL's alley as 'twodimensional array' is a native type of APL.


To begin with APL is completely unreadable. First of all, the character set used is not present in you favourite font. Secondly there is no operator prescedence in APL, Expressions are evaluated from right to left.

  • 3 + 4 = 7
    returns 3 (4 is not equal to 7 and returns 0, 3 is added)
  • 4 * 0 + 4
    returns 16 
  • etc.
Example code from wikipedia:

This code finds all the primenumbers up to R.Remember to read this from the right. ⍳R creates a vector  containing the numbers from 1 to R. 1↓ drops the first element of that vector. The new vector is assigned to R. The / in this context means a filter, it will filter the vector on the right by the binary vector(elements are boolean). 
R∘.×R produces a matrix(double sided array) containing the product of all combinations of numbers found in R. R∊ produces a binary vector of all elements in R that are also on the right. The binary vector is inverted by ~ and the filter is applied. Voila, a vector of prime numbers.

All in all it's great to learn a new language that is so different. This probably will never be language of choice but it has given me a different perspective on the other languages I use.

No comments:

Post a Comment