# Difference between revisions of "APL Wiki logo"

Miraheze>Adám Brudzewsky |
Miraheze>Marshall (Links and some editing) |
||

Line 1: | Line 1: | ||

[[File:APL Wiki Touch Square.png|thumb|right|APL Wiki logo]] | [[File:APL Wiki Touch Square.png|thumb|right|APL Wiki logo]] | ||

− | The APL Wiki logo can be seen as the following numeric matrix, where each number indicates the circle size. This page will explain, step-by-step, an expression<ref>[https://codegolf.stackexchange.com/users/78410/bubbler "Bubbler"], message [https://chat.stackexchange.com/transcript/message/52389201#52389201 "52389201"] in ''The Nineteenth Byte'' chat room. Stack Exchange network, 2019-10-31 23:57</ref> for this matrix — an expression which demonstrates quite a few APL features: | + | The APL Wiki logo can be seen as the following [[numeric]] [[matrix]], where each number indicates the circle size. This page will explain, step-by-step, an expression<ref>[https://codegolf.stackexchange.com/users/78410/bubbler "Bubbler"], message [https://chat.stackexchange.com/transcript/message/52389201#52389201 "52389201"] in ''The Nineteenth Byte'' chat room. Stack Exchange network, 2019-10-31 23:57</ref> for this matrix — an expression which demonstrates quite a few APL features: |

<source lang=apl> | <source lang=apl> | ||

⎕IO←0 | ⎕IO←0 | ||

Line 12: | Line 12: | ||

</source> | </source> | ||

− | We will follow APL's evaluation from right to left. | + | We will follow APL's evaluation from [[Evaluation order|right to left]]. |

== Counting == | == Counting == | ||

Line 31: | Line 31: | ||

=== Generating indices === | === Generating indices === | ||

− | The <source lang=apl inline>⍳</source> function takes a number ''N'' and [[index generator|generates indices]] until is has made ''N'' indices. Since we set <source lang=apl inline>⎕IO</source> to 0, we count from 0 until right before ''N'': | + | The <source lang=apl inline>⍳</source> function takes a number ''N'' and [[index generator|generates indices]] until is has made ''N'' [[Index|indices]]. Since we set <source lang=apl inline>⎕IO</source> to 0, we count from 0 until right before ''N'': |

<source lang=apl> | <source lang=apl> | ||

Line 40: | Line 40: | ||

== How many subsets? == | == How many subsets? == | ||

− | Consider a bag with four distinct items. If you stick your hand into the bag and pick two items out, how many different possibilities are there for which pair you get out? <math>\{(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)\}</math>. APL can tell you this with the <source lang=apl inline>!</source> function: | + | Consider a bag with four distinct items. If you stick your hand into the bag and pick two items out, how many different possibilities are there for which pair you get out? <math>\{(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)\}</math>. APL can tell you this with the [[Binomial]] (<source lang=apl inline>!</source>) function: |

<source lang=apl> | <source lang=apl> | ||

2!4 | 2!4 | ||

Line 46: | Line 46: | ||

</source> | </source> | ||

− | Notice how APL uses traditional mathematical symbols in a generalised way. The traditional post-fix (after its argument) symbol <math>!</math> is used with a syntax similar to how you'd normally use <math>+</math> or <math>×</math>. In fact, all APL functions can be used infix, like <math>a-b</math> or prefix, like <math>-b</math>. | + | Notice how APL uses traditional mathematical symbols in a generalised way. The traditional post-fix (after its argument) symbol <math>!</math> is used with a syntax similar to how you'd normally use <math>+</math> or <math>×</math>. In fact, all APL functions can be used [[Dyad|infix]], like <math>a-b</math> or [[Monad|prefix]], like <math>-b</math>. |

Anyway, how many sets of four could you pick? Obviously, only one; all the items: | Anyway, how many sets of four could you pick? Obviously, only one; all the items: | ||

Line 54: | Line 54: | ||

</source> | </source> | ||

=== Automatic mapping === | === Automatic mapping === | ||

− | A really nice feature of APL is its array-orientation. For computations which are defined on single elements, [[wikipedia:map (higher-order function)|map]]ping is implicit: | + | A really nice feature of APL is its array-orientation. For computations which are defined on single elements ([[scalar functions]]), [[wikipedia:map (higher-order function)|map]]ping is implicit: |

<source lang=apl> | <source lang=apl> | ||

0 1 2 3 4!4 | 0 1 2 3 4!4 | ||

1 4 6 4 1 | 1 4 6 4 1 | ||

</source> | </source> | ||

− | (What's up with picking zero out of four items? Since all empty hands are equal, there is exactly one such set — the empty set.) | + | (What's up with picking zero out of four items? Since all [[empty]] hands are equal, there is exactly one such set — the empty set.) |

== Order of evaluation == | == Order of evaluation == | ||

− | We want to generate the indices using <source lang=apl inline>⍳</source>… | + | We want to generate the indices using [[Iota]] (<source lang=apl inline>⍳</source>)… |

<source lang=apl> ⍳5!4 | <source lang=apl> ⍳5!4 | ||

| | ||

Line 73: | Line 73: | ||

== Swapping arguments == | == Swapping arguments == | ||

− | If the arguments of <source lang=apl inline>!</source> were swapped, we | + | If the arguments of <source lang=apl inline>!</source> were swapped, we wouldn't need that parenthesis. Enter the [[operator]] (higher-order function) [[swap]] (<source lang=apl inline>⍨</source>) which takes a [[dyadic]] function on its left and creates a new [[derived function]] which is identical to the original, but has swapped arguments: |

<source lang=apl> | <source lang=apl> | ||

4!⍨⍳5 | 4!⍨⍳5 | ||

Line 85: | Line 85: | ||

0.5 2 3 2 0.5 | 0.5 2 3 2 0.5 | ||

</source> | </source> | ||

− | Notice how we were dealing with integers until now, but then we multiply by a float (non-integer). In APL, you don't need to worry about numeric data type conversions. All numeric types get automatically promoted and demoted as needed. APL will usually use the most compact internal representation. | + | Notice how we were dealing with integers until now, but then we [[multiply]] by a float (non-integer). In APL, you don't need to worry about numeric data type conversions. All numeric types get automatically promoted and demoted as needed. APL implementations will usually use the most compact internal representation. |

=== Traditional mathematical symbols === | === Traditional mathematical symbols === | ||

− | Also notice that we use a proper multiplication symbol, <math>×</math>, for multiplication. If traditional mathematics has a symbol for a concept APL includes then APL will use that symbol. Another example is <math>÷</math> for division. | + | Also notice that we use a proper multiplication symbol, <math>×</math>, for multiplication. If traditional mathematics has a symbol for a concept APL includes then APL will use that symbol. Another example is <math>÷</math> for [[division]]. |

== Tables == | == Tables == | ||

Remember the multiplication table from school? | Remember the multiplication table from school? | ||

− | |||

<source lang=apl> | <source lang=apl> | ||

1 2 3 4 5∘.×1 2 3 4 5 | 1 2 3 4 5∘.×1 2 3 4 5 | ||

Line 100: | Line 99: | ||

5 10 15 20 25 | 5 10 15 20 25 | ||

</source> | </source> | ||

− | + | Any function can be made into a table with the [[Outer Product]]: | |

<source lang=apl> | <source lang=apl> | ||

1 2 3 4 5∘.+1 2 3 4 5 | 1 2 3 4 5∘.+1 2 3 4 5 | ||

Line 110: | Line 109: | ||

</source> | </source> | ||

=== Using an argument twice === | === Using an argument twice === | ||

− | It gets tedious to type the same argument twice. Enter the [[self]]ie operator which shares its symbol with the above-mentioned [[swap]] operator. There's no ambiguity here. ''Swap'' swaps the two arguments, while ''selfie'' uses a single argument twice: | + | It gets tedious to type the same argument twice. Enter the [[self]]ie operator which shares its [[Glyph|symbol]] with the above-mentioned [[swap]] operator. There's no ambiguity here. ''Swap'' swaps the two arguments, while ''selfie'' uses a single argument twice: |

<source lang=apl> | <source lang=apl> | ||

∘.+⍨1 2 3 4 5 | ∘.+⍨1 2 3 4 5 | ||

Line 129: | Line 128: | ||

</source> | </source> | ||

== Rounding == | == Rounding == | ||

− | The last step is to round these numbers down. Traditional mathematics writes ''floor'' as <math>⌊x⌋</math> but APL is regular, so no function is denoted by two separated symbols. If the function takes a single argument, then the symbol will be on the left, so we write floor as <source lang=apl inline>⌊x</source>: | + | The last step is to round these numbers down. Traditional mathematics writes ''floor'' as <math>⌊x⌋</math> but APL is regular, so no function is denoted by two separated symbols. If the function takes a single argument, then the symbol will be on the left, so we write [[floor]] as <source lang=apl inline>⌊x</source>: |

<source lang=apl> | <source lang=apl> | ||

⌊∘.+⍨.5×4!⍨⍳5 | ⌊∘.+⍨.5×4!⍨⍳5 |

## Revision as of 10:00, 5 November 2019

The APL Wiki logo can be seen as the following numeric matrix, where each number indicates the circle size. This page will explain, step-by-step, an expression^{[1]} for this matrix — an expression which demonstrates quite a few APL features:

```
⎕IO←0
⌊∘.+⍨.5×4!⍨⍳5
1 2 3 2 1
2 4 5 4 2
3 5 6 5 3
2 4 5 4 2
1 2 3 2 1
```

We will follow APL's evaluation from right to left.

## Contents

## Counting

### From 1 or from 0?

Whether to count from 0 or from 1 is an old disagreement among programmers. Many APLs let you choose whichever convention you want, but they tend to use 1 by default. To switch convention, we set the variable `⎕IO`

:

```
⎕IO←0
```

By the way, IO stands for Index Origin.

We can already now observe a couple of APL's characteristics…

### No reserved words

The name `⎕IO`

begins with the special Quad character (a stylised console) which symbolises the computer system itself. APL has no reserved words. Rather, all built-in constants, variables, functions and operators have the prefix `⎕`

indicating that they are part of the system. Because of this, we call them quad names.

### Assignments

- Assignment is not done with
`=`

like in many other programming languages, but rather with`←`

which also indicates the direction of the assignment: Whatever is on the right gets put into the name on the left.

### Generating indices

The `⍳`

function takes a number *N* and generates indices until is has made *N* indices. Since we set `⎕IO`

to 0, we count from 0 until right before *N*:

```
⍳5
0 1 2 3 4
```

## How many subsets?

Consider a bag with four distinct items. If you stick your hand into the bag and pick two items out, how many different possibilities are there for which pair you get out? . APL can tell you this with the Binomial (`!`

) function:

```
2!4
6
```

Notice how APL uses traditional mathematical symbols in a generalised way. The traditional post-fix (after its argument) symbol is used with a syntax similar to how you'd normally use or **Failed to parse (syntax error): {\displaystyle ×}**
. In fact, all APL functions can be used infix, like or prefix, like .

Anyway, how many sets of four could you pick? Obviously, only one; all the items:

```
4!4
1
```

### Automatic mapping

A really nice feature of APL is its array-orientation. For computations which are defined on single elements (scalar functions), mapping is implicit:

```
0 1 2 3 4!4
1 4 6 4 1
```

(What's up with picking zero out of four items? Since all empty hands are equal, there is exactly one such set — the empty set.)

## Order of evaluation

We want to generate the indices using Iota (`⍳`

)…

```
⍳5!4
```

That didn't work! This is because APL dispenses with traditional mathematics' confusing and inconsistent precedence order^{[2]}, replacing it with a simple right-to-left rule:

```
(⍳5)!4
1 4 6 4 1
```

## Swapping arguments

If the arguments of `!`

were swapped, we wouldn't need that parenthesis. Enter the operator (higher-order function) swap (`⍨`

) which takes a dyadic function on its left and creates a new derived function which is identical to the original, but has swapped arguments:

```
4!⍨⍳5
1 4 6 4 1
```

## A number is a number

The next step is to halve everything:

```
.5×4!⍨⍳5
0.5 2 3 2 0.5
```

Notice how we were dealing with integers until now, but then we multiply by a float (non-integer). In APL, you don't need to worry about numeric data type conversions. All numeric types get automatically promoted and demoted as needed. APL implementations will usually use the most compact internal representation.

### Traditional mathematical symbols

Also notice that we use a proper multiplication symbol, **Failed to parse (syntax error): {\displaystyle ×}**
, for multiplication. If traditional mathematics has a symbol for a concept APL includes then APL will use that symbol. Another example is **Failed to parse (syntax error): {\displaystyle ÷}**
for division.

## Tables

Remember the multiplication table from school?

```
1 2 3 4 5∘.×1 2 3 4 5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
```

Any function can be made into a table with the Outer Product:

```
1 2 3 4 5∘.+1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
```

### Using an argument twice

It gets tedious to type the same argument twice. Enter the selfie operator which shares its symbol with the above-mentioned swap operator. There's no ambiguity here. *Swap* swaps the two arguments, while *selfie* uses a single argument twice:

```
∘.+⍨1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
```

We'll use this in our logo expression:

```
∘.+⍨.5×4!⍨⍳5
1 2.5 3.5 2.5 1
2.5 4 5 4 2.5
3.5 5 6 5 3.5
2.5 4 5 4 2.5
1 2.5 3.5 2.5 1
```

## Rounding

The last step is to round these numbers down. Traditional mathematics writes *floor* as **Failed to parse (syntax error): {\displaystyle ⌊x⌋}**
but APL is regular, so no function is denoted by two separated symbols. If the function takes a single argument, then the symbol will be on the left, so we write floor as `⌊x`

:

```
⌊∘.+⍨.5×4!⍨⍳5
1 2 3 2 1
2 4 5 4 2
3 5 6 5 3
2 4 5 4 2
1 2 3 2 1
```

And there it is!

## References

- ↑ "Bubbler", message "52389201" in
*The Nineteenth Byte*chat room. Stack Exchange network, 2019-10-31 23:57 - ↑ K.E. Iverson, Appendix A: Conventions Governing Order of Evaluation, Elementary Functions: An Algorithmic Treatment). Science Research Associates, 1966