APL in 20 Minutes

Which flavour of APL?

All code in this article is supposed to work with APL2, APL+WIN, APLX, Dyalog APL and NARS2000. There are minor differences, these will be mentioned.

Run APL and use the session manager

As a starting point let us assume that an imaginary user has just started APL by selecting the appropriate command from the Windows "Start" menu.

What you get is APL's development environment, a so-called session-manager. Since APL is an interpreted language, you can type something into the session window and then press <Enter>. APL will try to evaluate your expression and display the result, or it will tell you that something is wrong. The symbol , for obvious reasons called "lamp", indicates a comment: anything on the right of a lamp character is therefore ignored by the interpreter.

Here you can see some simple examples. Input lines are indented by 6 characters, the interpreter's response starts on the left:

      1+2
3
      1+2×3
7
      2×3+1
8 
⍝ ???
⍝ Well, there is only *one* single precedence rule in APL: processing starts from the right
      (2×3)+1
7
⍝ But parenthesis are processed first
⍝ Let us create a variable "int":
      int←1
      int+2
3
      int←3 4 8
      int+1
4 5 9
⍝ APL is an array language:
      int+int
6 8 16

Okay, we got a first impression.

APL: powerful, short, concise

It is believed that with APL even complex problems can be solved in some lines, if not a single one. Is this really true?

I suggest that we solve a small (but not too small) real problem: Let us take the source code of a web page and remove the entire HTML tags from this. As a result we expect to see the real content of the page and nothing else. How fast can this be done?

The task

To make life a bit easier we are going to deal with well-formed code only. So let's take code from a website which is definitely supposed to provide well-formed code, although most HTML pages on the web are still syntactically incorrect:

http://www.w3.org/

You should see something similar to this in your browser window:

w3c.gif

The contents of that page may be read into APL2, APL+Win, APLX, Dyalog and NARS200 as follows:

  1. Goto http://www.w3.org

  2. Save the contents of that page to a file (say C:\World Wide Web Consortium (W3C).htm) (This is done slightly differently for each browser)
  3. Within the APL interpreter:

    tn←'C:\World Wide Web Consortium (W3C).htm' ⎕ntie 0
    myHtml←⎕nread tn
    ⎕nuntie tn
    myHtml←(myHtml≠⎕av[10])⊂myHtm

Please check that ⎕AV[10] is the newline character with your favorite APL interpreter.

Examine the HTML code

Let's examine the length of the variable we have just created. For this purpose there is a function called "shape", represented by the symbol, which is in fact the Greek "rho" character. Generally, in APL a function may take one argument or two arguments or no argument at all. In our case it is exactly one argument, the variable. The variable then must be specified to the right of the function:

      ⍴myHtml
648      

The result is possibly not exactly the same when this is done right now because the page might have changed in the meantime.

The 648 represents the number of lines (or records) in the file the source code was saved in.

To find out the length of each of the strings in the 648 items of myHtml we need to introduce APL operators, a concept that is radically different from anything called "operator" elsewhere. In APL, an operator takes at least one function as an operand. It then creates a so-called "derived function" by applying operator-specific rules to that function or functions.

Sounds impressive and means nothing to you? Well, let's try to work out what that means in practice. APL comes with an operator "Each", represented by the ¨ symbol. This operator takes its operand and applies it to all elements of the array provided to the right.

To find out the length of every single string in myHtml we have to provide the function to every single item in that variable:

      ⍴¨myHtml
      38  109  73  122  96  0  359  668  0  40  81  46  70  67  51  55  0  59  57  57  59  61 141  7  0  6  114  0  61 0  5  66  20  0  661  222  0  111  6  6  6  0  128  70  62  68  
...

In fact this is a loop, executed exactly 648 times, but we do not need to know this, or to care about it. <Ruby value="ignore">Every programmer must get exited right now!</Ruby>

Operators

Let us deviate from the main point for a moment and introduce another operator.

The operator "reduce", represented by the / character, is defined as "take its operand (the function) and put it between all the items of the array passed to the derived function". The expression:

      +/1 2 3 4 5 6 7

therefore means that according to the rules we have just defined, APL will build up this:

      1+2+3+4+5+6+7
28

You might have an idea how extraordinary powerful this concept is, since you can specify any function here which fits, including self-defined ones.

Extract content from Code

The strategy

Now we want to get the content from the code. For this we need a strategy:

So the strategy is easy: build up a mask of Booleans which allows us to get rid of all the HTML stuff. Let's work out how we can do this in APL. In the first step, we create a small string we can play with:

      testVars←'<p style="font-size: medium">Hello, world</p><p>There we go</p>'

Find start points and end points

In first place, we need to know where the < and the > characters are. For this, we have to master the "membership" function, represented by the Greek character

It does exactly what the name suggests: it looks for every element in the left argument if it is contained in the right argument. If that is the case, a 1 is returned, otherwise a 0:

      testVars∊'<>'
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1

As you can see, Booleans are represented by 1 (true) and 0 (false) in APL.

For the next step we need another operator which is very close to the "reduce" operator we already met: it is called "expand", which uses the symbol \. Let us start with some expressions to get familiar with this new operator:

      +/1 2 3 4  ⍝ Reduce: APL makes 1+2+3+4 from this
10
      +\1 2 3 4  ⍝ Expand: APL makes (1) (1+2) (1+2+3) (1+2+3+4) from this 
1 3 6 10     

Expand returns a result for every single step. Let's follow the interpreter step by step:

  1. The first item (the 1) is taken and printed
  2. The first and the second item are added up and the result (3) is printed
  3. The result we just got (3) is taken and added to the third element which results to 6
  4. The result we just got (6) is taken and added to the forth element which results to 10

So far so good. Let's try the same thing with the function. This is a very simple function that takes a left and a right argument and checks them for being different. The result is a Boolean:

      0≠0
0
      1≠0
1
      1≠1
0

In APL, you can use Booleans in arithmetic operations:

      3+9=9
4

First 9=9 is processed (right-to-left!) which returns a 1 for true and then the 1 is added to 3.

Let's use the membership function to find out where the < and > are located:

      testVars∊'<>'
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1

And now we put some magic in place:

      ≠\testVars∊'<>'
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0

That is a big step forward. Let's look into the details:

      ≠\1 0 0 0 1 0 1
1 1 1 1 0 0 1

According to the rule we just worked out, the "expand" operator \ performs the following steps:

  1. Take the first item (1) and print it
  2. Take the first and second item and pass them as left and right argument to the function, here ≠; That leads to 1 ≠ 0 which is true, so a 1 is returned.
  3. Take the last result (1) and take the third item: 1 ≠ 0 results in 1
  4. Take the last result (1) and take the forth item: 1 ≠ 0 results in 1
  5. Take the last result (1) and take the fifth item: 1 ≠ 1 results in 0
  6. Take the last result (0) and take the sixth item: 0 ≠ 0 results in 0
  7. Take the last result (0) and take the seventh item: 0 ≠ 1 results in 1

Now we can use the vector of Booleans we got to mask the HTML code. For this we use again "reduce" (/), but this time we do not provide a function as left operand but the vector of Booleans. An easy example:

      1 0 0 1 1/'abcde'
ade

As you can see, items associated with a 1 are still represented in the result while those associated with a zero are not. We can use this to do:

      (≠\testVars∊'<>')/testVars
<p style="font-size: medium"</p<p</p

Upps. We where looking for the content, not the HTML code. We have to negate the Booleans. This can be done with the function ~ :

      ~1
0     
      ~0
1      
      (~≠\testVars∊'<>')/testVars   
>Hello, world>>There we go>

We are almost there. Only that the starting character of any piece of HTML code has survived is still a problem. In the next step the Boolean "or" function is used to solve this problem

      ≠\1 0 0 0 1 0 1
1 1 1 1 0 0 1
      a∨≠\a←1 0 0 0 1 0 1
1 1 1 1 1 0 1

We use these techniques now to define a function:

 [0]  r←enclosedBy Strip string;mask
 [1]  mask←string∊enclosedBy
 [2]  mask←~mask∨≠\mask
 [3]  r←mask/string

Now we have a function "Strip". The vector of Booleans is assigned to the variable "mask" which is a local one. That means that this variable exists only inside the "Strip" function when the code is executed.

Problem solved

Let us test our newly created function:

      testVars ⍝ calling the name of a variable simply prints its value
<p style="font-size: medium">Hello, world</p><p>There we go</p>
      '<>' Strip testVars
Hello, worldThere we go

Looks good. Now let's perform this function to all items from the w3c's website with the help of the "each" operator and assign the result to a variable "content". We then will print the result to the session:

      content←(⊂'<>')Strip¨myHtml

ed_2.gif

The result obviously contains many empty lines. If you are surprised: any line in the source code which contains nothing but HTML tags is now empty. This is true for all the lines containing <meta> tags, for example.

So let's remove all empty lines in a last step without looking into the details. The following statement removes all blanks from every single item in content and then checks the length of the rest. Only items with a length greater 0 will survive:

      content←(0<∊⍴¨content~¨' ')/content
      ⎕←content

However, for this document we shall use this command:

      )ed content

in order to display the variable in an editor window. This command is Dyalog specific. Most other APL dialects have similar commands to achieve the same.

Having executed that expression, the contents of the editor window has changed:

ed_3.gif

Make it general

A last remark: the function "Strip" is able to do more than simply remove HTML code. Thanks to the fact that we can provide a left argument which will override the default (<>) we can use the function for other purposes as well:

      v←'This is some nonsense (written by me) you can forget'
      '()'Strip v
This is some nonsense  you can forget

Created by KaiJaeger


CategoryGuides

AplIn20Minutes (last edited 2014-02-14 19:34:39 by KaiJaeger)