# Pervasive Functions (8 of 9)

APL's primitive (built-in) functions fall into two classes, scalar functions and mixed functions. Scalar functions are defined on scalar arguments and extend to arrays on an element-by-element basis. Mixed functions are defined on arrays and may yield arrays which are different in shape or rank from their arguments.

Scalar functions include + - × ÷ | ⌈ ⌊ * ⍟ ○ ! ^ ∨ ⍲ ⍱ < ≤ = ≥ > ≠ and the monadic forms of ~ ?

Mixed functions include ⍴ , ⍪ ⌽ ⊖ ⍉ ↑ ↓ / ⌿ \ ⍀ ⍳ ∊ ⍋ ⍒ ? ⌹ ⊥ ⊤ ⍕ ⍎ ⊂ ⊃ ≡ ⍷ ⌷ and the dyadic forms of ~ ?

If one of the arguments to a dyadic scalar function is a scalar, the scalar is applied to each item of the other argument - a property known as scalar extension. The other important property of scalar functions is that they are pervasive - that is, they apply at all levels of nesting. Monadic scalar functions are applied independently to every simple scalar in their argument, and the result retains the structure of the argument. Dyadic scalar functions are applied independently to corresponding pairs of simple scalars in the arguments. A simple scalar argument will extend to each simple item in the other argument. In other cases, the arguments should have similar structure. For simple arrays, the function applies between corresponding items. For nested arrays, the pairing of corresponding items continues until the function can be applied to simple arguments.

```     ((1 2 3) (4 5 6)) (7 8 9 10) + (10 100) (4⍴⊂⍳3)
11 12 13  104 105 106   8 9 10  9 10 11  10 11 12  11 12 13```

In the example above, the left argument to + is a two element vector, as is the right argument:

```      ⎕DISPLAY ((1 2 3) (4 5 6)) (7 8 9 10)
┌→───────────────────────────────┐
│ ┌→────────────────┐ ┌→───────┐ │
│ │ ┌→────┐ ┌→────┐ │ │7 8 9 10│ │
│ │ │1 2 3│ │4 5 6│ │ └~───────┘ │
│ │ └~────┘ └~────┘ │            │
│ └∊────────────────┘            │
└∊───────────────────────────────┘
⎕DISPLAY (10 100) (4⍴⊂⍳3)
┌→─────────────────────────────────────────────┐
│ ┌→─────┐ ┌→────────────────────────────────┐ │
│ │10 100│ │ ┌→────┐ ┌→────┐ ┌→────┐ ┌→────┐ │ │
│ └~─────┘ │ │1 2 3│ │1 2 3│ │1 2 3│ │1 2 3│ │ │
│          │ └~────┘ └~────┘ └~────┘ └~────┘ │ │
│          └∊────────────────────────────────┘ │
└∊─────────────────────────────────────────────┘```

The first element of the left argument is, in turn, two three-element vectors. These are paired with the first element of the right argument, a simple two-element vector. The respective additions which take place are thus:

`      1 2 3+10`

...and...

`      4 5 6+100`

The second element of the left argument is a simple four-element vector which is paired with the second element of the right argument, a four-element nested vector. A series of additions take place, the first of which is:

`       7+1 2 3`

...and so on.



LearnMoreApl/PervasiveFunctions (last edited 2017-02-16 18:50:29 by KaiJaeger)