## page was renamed from AplIn20Minutes = APL in 20 Minutes = <> == Which flavour of APL? == In principle, all code in this article is supposed to work with APL2, APL+WIN, 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 . 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 provides well-formed code, although most HTML pages on the web are still syntactically incorrect: http://download.aplwiki.com/apltree/ You should see something similar to this in your browser window: {{attachment:sampleHtml.png}} The contents of that page may be read into APL2, APL+Win, Dyalog and NARS200 as follows: 1. Goto http://download.aplwiki.com/apltree/LatestStableVersions/ 1. Save the contents of that page to a file (say C:\myhtml.htm) (This is done slightly differently for each browser) 1. Within the APL interpreter: {{{ tn←'C:\myhtml.htm' ⎕ntie 0 conversionCode←82 noOfBytes←⎕NSIZE ¯1 startAt←0 myHtml←⎕NREAD tn, conversionCode, noOfBytes, startAt myHtml←(myHtml≠(⎕UCS 13))⊂myHtml }}} Note that this code works in Dyalog. It might need some minor changes in other interpreters. Note that `⎕UCS 13` produces a new line character in Dyalog. If you use a different interpreter check your documentation where in `⎕AV` you can find this character. === 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 199 }}} The result is possibly not exactly the same when this is done right now because the page might have changed in the meantime. The 199 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 199 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 184 103 23 37 80 135 2623 16 8 12 133 31 6 17 5 7 20 88 25 88 77 38 4 313 20 20 135 7 26 8 5... }}} In fact this is a loop, executed exactly 199 times, but we do not need to know this, or to care about it. Every programmer must get exited right now! === 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: * We know that the < and > characters represent HTML tags, because these characters have to be encoded if they are contained in the content itself. * We therefore also know that everything '''between''' < and > is not of any interest to us, including < and > itself. * We can ignore the fact that !JavaScript code would look like code following the two rules we just worked out, because the page does not contain any !JavaScript. 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←'

Hello, world

There we go

' }}} === 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 1. The first and the second item are added up and the result (3) is printed 1. The result we just got (3) is taken and added to the third element which results to 6 1. 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 1. 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. 1. Take the last result (1) and take the third item: 1 ≠ 0 results in 1 1. Take the last result (1) and take the forth item: 1 ≠ 0 results in 1 1. Take the last result (1) and take the fifth item: 1 ≠ 1 results in 0 1. Take the last result (0) and take the sixth item: 0 ≠ 0 results in 0 1. 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

')/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

Hello, world

There we go

'<>' Strip testVars Hello, worldThere we go }}} Looks good. Now let's perform this function to all items from the `myHtml` variable 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 }}} {{attachment:ed_2.png}} 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 tags, for example. Strictly speaking however the lines are not empty since every line still holds a line feed character at the moment. With what we've learned so far we can remove this Line Feed characters from each record: {{{ myHtml←myHtml~¨⎕AV[2+⎕IO] }}} Now we are ready to 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: {{attachment:ed_3.png}} == 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