Introduction to APL: Difference between revisions
No edit summary |
No edit summary |
||
Line 9: | Line 9: | ||
It would produce the same answer no matter what programming language you use. | 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 | 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 | : K == 5 + 6 + 8 + 9 + 12 +14 | ||
In APL, you'd do the same thing with | In APL, you'd do the same thing with | ||
: K {:=} +/5 6 8 9 12 14 | : 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<br/> | |||
<code> | |||
k == 0;<br/> | |||
for (i=0;i++;i<1000) {<br/> | |||
k += X[i];<br/> | |||
}</code><br> | |||
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. |
Revision as of 17:34, 22 February 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
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=0;i++;i<1000) {
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.