Introduction to APL

From NARS2000
Revision as of 11:22, 8 February 2016 by Robert Wallick (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Welcome to the wonderful world of APL. Whether you've never done programming before or you're a journeyman programmer, APL is going to be something new to you. APL works like no other programming language. First, if you've done any programming, you "know" that when you have an assignment or something like a piece of code is executed, it's "always" done right to left and mathematical formulas are always evaluated right to left according to precedence, so that exponentiation is done first, then multiplication and division, then addition and subtraction. Well, APL is going to blow you away because everything you know is wrong.

First, APL is "backwards" to what you know. APL operates from right to left. All other programming languages operate from left to right. When it comes to computing mathematical formulas, APL has no precedence; unless you put something in parenthesis, all mathematical computations are done strictly right to left to produce the result.

Also, APL uses a specialized set of symbols, and before we had graphical systems, it even had its own specialized keyboard. Many of the actions you have to write a program in other languages can be done in one character in APL. While your typical programming language would show 3 times 4 as 3*4, APL has a multiply symbol, so it is written as 3×4, and to create one half or 1/2 in most languages, in APL you would enter 1÷2 .

So, for example, if you have the following mathematical expression

4*5-10 (in normal programming languages), or in APL: 4 × 5 - 10

Your typical programming language would compute it as 4×5 which is (20), then 20 - 10, resulting in 10. APL will compute it as 5-10 (-5) * 4, or -20. You can adjust this by placing arguments within parenthesis. If your expression was written as

(4 × 5) - 10

It would produce the same answer no matter what programming language you use.

Where APL shines is its ability to handle arrays as easily as most programs handle single values. For example, the plus reduce operator is used to sum a list of numbers, so in a typical programming language like C or C++ you'd have

K == 5 + 6 + 8 + 9 + 12 +14

In APL, you'd do the same thing with

K ← +/5 6 8 9 12 14

Now, that by itself isn't very exciting. But what if you had an array X of 1000 numbers. In C you'd have to do something like
     k == 0;
     for (i=1;i++;i<1001) {
          k += X[i];
     }

You'd do the same thing in APL with

k ← +/ X

APL will take all of the entries in the array X and sum them, in one instruction. You don't even have to know how many entries are in the array X; APL will handle it for you.

To be able to program in APL you'll have to learn what all of the operators do. Links to pages describing APL/APL2 symbols and what they do, just below: