Introduction to APL: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
No edit summary
Line 22: Line 22:
: k {:=} +/ X
: 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.
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. A link to a page describing each symbol and what it does, is shown below.
-- To be added --

Revision as of 00:19, 1 March 2013

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, 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 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. A link to a page describing each symbol and what it does, is shown below.

-- To be added --