2 3 + 4 5

2+3 4 5

2 3 + 3 4 5

⍴(5 6⍴⍳30)×5 6⍴⍳30

2-3+4

(2-3)+4

2 3⍴⍳6

2↑⍳6

2↓⍳6

)pcopy display

DISPLAY⊂2 3⍴⍳6

DISPLAY ⊂2 3⍴⊂⍳6

⎕ml←3

⊃'abc' 'def' 'ghi'

⎕ml←0

⊃'abc' 'def' 'ghi'

⎕ml←3

2⌷'abcde'

1 2⌷4 5⍴⍳20

(1 2) (3 4)⌷4 5⍴⍳20

1 (2 3)⌷4 5⍴⍳20

2⌷'abc' 'def' 'ghi' 'jkl'

2⊃'abc' 'def' 'ghi' 'jkl'

(2⊃'abc' 'def' 'ghi' 'jkl')≡⊃2⌷'abc' 'def' 'ghi' 'jkl'

0 1 1 0 0/⍳5

- Primitive functions are the "doing" building blocks of APL - "verbs" in the grammar analogy.
- Two types:
- Scalar functions use a one-to-one relationship of left and right arguments - they work on the scalar elements.
- Mixed functions have more complex behaviour, typically the left argument is used to control the transformation of data elements in the right argument.
- Scalar functions automatically make left- or right- scalar arguments conform to the other argument.
- APL has no functional hierarchy - all functions (including those you define yourself) are equal.
- The order of execution is determined by the rule that the right argument of a function is the result of the entire expression to the right of the function. One of the conventions of mathematics, for example log sin x.
- Notice how this execution rule leads to natural left-to-right reading )sometimes misleadingly referred to as "right to left execution).
- You can force disruption of left-to-right reading by using parentheses.
- Don't overuse parentheses, they have other - more important - uses than breaking up reading flow.
- There's no benefit in writing expressions that are too complicated for you to read yourself. If it's that hard, do it on more than one line.
- Be alert to emulation level, it can make the same "appearance" do different things.
- The indexing function is "purer" than the older bracket-indexing; and has advantages.
- Note the difference between "index" and "pick".
- There's a convention that 1 means "true" and 0 means "false" - good to carry this through to expressions you write yourself.
- Indexing isn't the only way to work on selected elements, selection is often more useful and general.

Further Reading:

- Primitive Functions - Dyalog Language Reference
- Scalar Functions
- Mixed Functions

Exercises:

- "Puzzle of the Year" - using all four digits of the year (in order), write expressions to generate every integer from 1 to 100 inclusive. Calculate the score for each expression as the number of characters (including spaces) - for example ×2008 generates 1 and has a score of 5, ⌊20÷08 generates 2 and has a score of 6.
- Write an expression which replaces all values greater than 20 in a random vector of positive integers with the value 0
- Write a series of expressions to do the same value substitution in an arbitrarily-shaped array.
- Write an expression which determines whether a simple vector consists of numbers or characters.
- Write an expression which partitions a random integer vector into parts beginning with even numbers = for example 1 2 3 3 4 5 would become (1) (2 3 3) (4 5). How does your expression handle succesive even numbers? What if there are no even numbers?