Linux Format

APL EXAMPLE

-

To get more of a feel for APL, try entering enter the following into an APL implementa­tion (see Hands On APL box, page 92):

n. n 10 . It’ll cause the following to be displayed, which you’ll recognise as a 10 x 10 multiplica­tion table.

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

6 12 18 24 30 36 42 48 54 60

7 14 21 28 35 42 49 56 63 70

8 16 24 32 40 48 56 64 72 80

9 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100

Not exactly a difficult exercise, but in virtually all other languages it would have involved a couple of loops and more than nine characters. Let’s see how it works. APL expression­s are interprete­d right to left, so type 10 and, because is the index generator function, it generates all the numbers from one to the value to its right, so you see all numbers from 1 to 10. Now try n 10 and APL doesn’t respond, because is the assign function, so it just assigns the value to its right to the variable name to its left. In this case, it assigns the numbers from 1 to 10 to which is, therefore, a vector – a one-dimensiona­l array. To prove that, type n , and again APL responds with the numbers from 1 to 10. The next part is trickier because, although , . and have different meanings individual­ly, the combinatio­n . defines a function called the outer product. This is an array function, which multiplies each value in the array before it ( ) by every value in the array after it (also ). As the arrays are one-dimensiona­l the result is a two-dimensiona­l array, and APL formats it as such.

Newspapers in English

Newspapers from Australia