First functions

Dyalog
APL
Glyphs
Author

Jeremy Howard

Published

July 5, 2022

]box on -style=max -trains=tree -fns=on
┌→─────────────────────────────────────┐
│Was ON -style=max -trains=tree -fns=on│
└──────────────────────────────────────┘

Boolean

= (Equal sign)

dyadic = (Equal to)

= is equal and returns a boolean (true/false). 1 means true, 0 means false.

1 = 1
 
1
 

= works elementwise and will broadcast as needed.

1 = 1 2
┌→──┐
│1 0│
└~──┘
1 = 1 1
┌→──┐
│1 1│
└~──┘

We can also compare characters

'Banana' = 'aaaaaa'
┌→──────────┐
│0 1 0 1 0 1│
└~──────────┘
'Banana' = 'a'
┌→──────────┐
│0 1 0 1 0 1│
└~──────────┘
'Banana' 'Apple' 'Candy' = 'a'
┌→──────────────────────────────────────┐
│ ┌→──────────┐ ┌→────────┐ ┌→────────┐ │
│ │0 1 0 1 0 1│ │0 0 0 0 0│ │0 1 0 0 0│ │
│ └~──────────┘ └~────────┘ └~────────┘ │
└∊──────────────────────────────────────┘

(Not equal)

monadic (Unique Mask)

Monadic returns 1 on the first occurrence of an item in an array.

≠22 10 22 22 21 10 5 10
┌→──────────────┐
│1 1 0 0 1 0 1 0│
└~──────────────┘
≠ 'Banana'
┌→──────────┐
│1 1 1 0 0 0│
└~──────────┘
≠ 'Mississippi'
┌→────────────────────┐
│1 1 1 0 0 0 0 0 1 0 0│
└~────────────────────┘

dyadic (Not Equal To)

Dyadic returns true (1) if elements are not equal, and false (0) if elements are equal.

1 2 3 ≠ 4 2 ¯1
┌→────┐
│1 0 1│
└~────┘

The number 7 and the character 7 are not equal.

7 ≠ '7'
 
1
 

< (Less than sign)

dyadic < (Less than)

1 2 3 < 4 2 ¯1
┌→────┐
│1 0 0│
└~────┘
1 2 3 < 2
┌→────┐
│1 0 0│
└~────┘

> (Greater than sign)

dyadic > (Greater than)

1 2 3 > 4 2 ¯1
┌→────┐
│0 0 1│
└~────┘
1 2 3 > 2
┌→────┐
│0 0 1│
└~────┘

(Less than or equal to sign)

dyadic (Less than or equal to)

1 2 3 ≤ 4 2 ¯1
┌→────┐
│1 1 0│
└~────┘
1 2 3 ≤ 2
┌→────┐
│1 1 0│
└~────┘

(Greater than or equal to sign)

dyadic (Greater than or equal to)

1 2 3 ≥ 4 2 ¯1
┌→────┐
│0 1 1│
└~────┘
1 2 3 ≥ 2
┌→────┐
│0 1 1│
└~────┘

(Equal underbar)

monadic (Depth)

Depth shows how nested an array is. A simple scalar is an array with no nesting.

≡ 7
 
0
 

An array of simple scalars has depth 1.

≡ 'abc'
 
1
 

An array that is an array of simple arrays has depth 2.

(1 2)(3 4)
┌→────────────┐
│ ┌→──┐ ┌→──┐ │
│ │1 2│ │3 4│ │
│ └~──┘ └~──┘ │
└∊────────────┘
≡ (1 2)(3 4)
 
2
 

If the depth is not consistent, then it returns the max depth as a negative number.

(1 2)(3 4) (5 (6 7))
┌→────────────────────────┐
│ ┌→──┐ ┌→──┐ ┌→────────┐ │
│ │1 2│ │3 4│ │   ┌→──┐ │ │
│ └~──┘ └~──┘ │ 5 │6 7│ │ │
│             │   └~──┘ │ │
│             └∊────────┘ │
└∊────────────────────────┘
≡ (1 2)(3 4) (5 (6 7))
  
¯3
  

dyadic (Match)

Match does a comparison to see if 2 objects are equal. It is similar to =, but it works on entire objects rather than elementwise. In other words it is equal with a wider scope.

1 ≡ 1
 
1
 
1 ≡ 0
 
0
 

works on whole objects and not elementwise with broadcasting.

1 ≡ 1 1
 
0
 

(Equal Underbar Slash)

monadic (Tally)

Tally counts the major cells in an array. This means it gives the length of the leading axis. For a vector, or rank 1 array, this is the same as it’s shape but as a scalar.

≢ 1 2 3
 
3
 
⍴ 1 2 3
┌→┐
│3│
└~┘

If there are multiple dimensions, Tally returns the number of major cells, which is the size of the first dimension

≢ 2 3 ⍴ ⍳6
 
2
 
≢ 3 2 ⍴ ⍳6
 
3
 

dyadic (Not match)

Not match does a comparison to see if 2 objects are not equal. It is similar to , but it works on entire objects rather than elementwise. In other words, it is equal with a wider scope.

1 ≢ 1
 
0
 
1 ≢ 0
 
1
 

works on whole objects and not elementwise with broadcasting.

1 ≢ 1 1
 
1
 

(Logical or)

dyadic (Greatest Common Divisor (Or))

Standard or operator when applied to booleans.

0 1 0 1 ∨ 0 0 1 1  ⍝ Truth table for *or*
┌→──────┐
│0 1 1 1│
└~──────┘

5 is the largest number that divides 15 and 35 cleanly (meaning no remainder). Therefore, 5 is the greatest common divisor of 15 and 35.

15 1 2 7 ∨ 35 1 4 0  ⍝ GCD
┌→──────┐
│5 1 2 7│
└~──────┘

(Logical NOR)

dyadic (Nor)

For more details on logical nor, see this page

0 1 0 1 ⍱ 0 0 1 1  ⍝ Truth table for *nor*
┌→──────┐
│1 0 0 0│
└~──────┘

(Logical AND)

dyadic (Lowest Common Multiple (And))

Standard and operator when applied to booleans.

0 1 0 1 ∧ 0 0 1 1  ⍝ Truth table for *and*
┌→──────┐
│0 0 0 1│
└~──────┘

105 is the smallest multiple of both 15 and 35, therefore 105 is the lowest common multiple of 15 and 35.

15 1 2 7 ∧ 35 1 4 0  ⍝ LCM
┌→────────┐
│105 1 4 0│
└~────────┘

~ (Logical NOT)

monadic ~ (Not)

~ 0 1  ⍝ Truth table for *not*
┌→──┐
│1 0│
└~──┘

dyadic ~ (Without;Excluding)

Gives the elements from the left argument that are not in the right.

3 1 4 1 5 ~ 5 1
┌→──┐
│3 4│
└~──┘

Also works on the character vectors

'aa' 'bb' 'cc' 'bb'  ~ 'bb' 'xx'
┌→──────────┐
│ ┌→─┐ ┌→─┐ │
│ │aa│ │cc│ │
│ └──┘ └──┘ │
└∊──────────┘

Also works on nested arrays

(1 2) 3 ~ ⊂1 2
┌→┐
│3│
└~┘

(Logical NAND)

dyadic (Nand)

Nand is “Not and”

0 1 0 1 ⍲ 0 0 1 1  ⍝ Truth table for *nand*
┌→──────┐
│1 1 1 0│
└~──────┘

We could use and (^) apply not (~) afterward to get the same result. As you can see it truly is “Not and”

~ 0 1 0 1 ∧ 0 0 1 1  ⍝ Truth table for *nand*
┌→──────┐
│1 1 1 0│
└~──────┘

/ (Slash)

monadic / (Replicate)

v←22 10 22 22 21 10 5 10
≠v
┌→──────────────┐
│1 1 0 0 1 0 1 0│
└~──────────────┘
(≠v)/v
┌→─────────┐
│22 10 21 5│
└~─────────┘
3 1 3 1 3 1 / 'Banana'
┌→───────────┐
│BBBannnannna│
└────────────┘

Min, max, index, concat

(iota)

monadic (index generator)

Creates index locations for array with specified shape.

⍳4
┌→──────┐
│1 2 3 4│
└~──────┘
⍳2 3
┌→──────────────────┐
↓ ┌→──┐ ┌→──┐ ┌→──┐ │
│ │1 1│ │1 2│ │1 3│ │
│ └~──┘ └~──┘ └~──┘ │
│ ┌→──┐ ┌→──┐ ┌→──┐ │
│ │2 1│ │2 2│ │2 3│ │
│ └~──┘ └~──┘ └~──┘ │
└∊──────────────────┘

is often used with to create arrays

2 3 ⍴ ⍳6
┌→────┐
↓1 2 3│
│4 5 6│
└~────┘

To generate an array with various patterns you can combine with other functions.

1+2×⍳6
┌→────────────┐
│3 5 7 9 11 13│
└~────────────┘

Array of shape 0 is nan empty vector and the index locations of a shape 0 array is and empty vector.

⍳0
┌⊖┐
│0│
└~┘

dyadic (index of)

Provides the index locations for where (right argument) is in (left argument)

1 3 6 5 4 ⍳ 3
 
2
 
'ABCDABCDEF' ⍳ 'ACFG'
┌→────────┐
│1 3 10 11│
└~────────┘
⎕←mat←3 2 ⍴ ⍳6
┌→──┐
↓1 2│
│3 4│
│5 6│
└~──┘

works on major cells, or along the first dimension. The third major cell of mat is 5 6.

mat←3 2 ⍴ ⍳6
mat ⍳ 5 6
 
3
 

(iota underbar)

monadic (Where)

When applied to a boolean/binary array, where gives index locations of the true values.

⍸ 1 0 0 1 1
┌→────┐
│1 4 5│
└~────┘

When applied to a natural number, where repeats the index locations if the number is greater than 1. For example ⍸1 2 3 would give 1 2 2 3 3 3

⍸1 2 3
┌→──────────┐
│1 2 2 3 3 3│
└~──────────┘
⍸ 2 0 0 2 1
┌→────────┐
│1 1 4 4 5│
└~────────┘

When applied to a higher rank array it still provides the index locations of the elements, but index locations have multiple values (in this case row and column).

⎕←bmat ← 2 3 ⍴ 0 1 0 1 0 1
⍸ bmat
┌→────┐
↓0 1 0│
│1 0 1│
└~────┘
┌→──────────────────┐
│ ┌→──┐ ┌→──┐ ┌→──┐ │
│ │1 2│ │2 1│ │2 3│ │
│ └~──┘ └~──┘ └~──┘ │
└∊──────────────────┘
⎕←bmat ← 2 3 ⍴ 0 2 0 2 0 2
⍸ bmat
┌→────┐
↓0 2 0│
│2 0 2│
└~────┘
┌→────────────────────────────────────┐
│ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ │
│ │1 2│ │1 2│ │2 1│ │2 1│ │2 3│ │2 3│ │
│ └~──┘ └~──┘ └~──┘ └~──┘ └~──┘ └~──┘ │
└∊────────────────────────────────────┘

dyadic (Interval Index)

Interval index creates intervals and returns an array that tells you which interval a value falls in.

If the following example, is 2 4 6. Therefore the following intervals (sometimes called bins) are created.

  • 0th interval: Less than 2
  • 1st interval: 2-4 (excluding 4)
  • 2nd interval: 4-6 (excluding 6)
  • 3rd interval: Greater than or equal to 6

(right argument) is 1 2 3 4 5 6 7 so let’s put those into intervals, which the first interval starting at 0.

  • The first value of is 1. 1 is Less than 2. Therefore it is in the 0th interval and interval index returns 0.
  • The second value of is 2. 2 is in the 2-4 range. Therefore it is in the 1st interval and interval index returns 1.
  • The third value of is 3. 3 is in the 2-4 range. Therefore it is in the 1st interval and interval index returns 1.
  • The fourth value of is 4. 4 is in the 4-6 range. Therefore it is in the 2nd interval and interval index returns 2.

And so on and so forth.

2 4 6 ⍸ 1 2 3 4 5 6 7
┌→────────────┐
│0 1 1 2 2 3 3│
└~────────────┘

Interval index works the same way with character vectors.

  • D is in the first interval because it is between A and E.
  • Y is in the last interval (fifth) because it is larger than all values
  • A is in the first interval because it is between A and E.
  • L is in the third interval because it is between I and O.

etc.

'AEIOU' ⍸ 'DYALOG'
┌→──────────┐
│1 5 1 3 4 2│
└~──────────┘

Interval index works across major cells.

⎕←mat←3 2⍴⍳6
┌→──┐
↓1 2│
│3 4│
│5 6│
└~──┘
mat ⍸ 3 3
 
1
 
mat ⍸ 3 5
 
2
 
mat ⍸ 2 2 ⍴ 3 3 3 5
┌→──┐
│1 2│
└~──┘

(Upstile)

monadic (Ceiling)

Rounds up to nearest whole number.

⌈ 3.4 ¯3.4 3 0
┌→───────┐
│4 ¯3 3 0│
└~───────┘

dyadic (Maximum)

Takes maximum of 2 values

3⌈2
 
3
 
3 2⌈2 3
┌→──┐
│3 3│
└~──┘
4 ⌈ 6 ⌈ 2
 
6
 
a ← ¯4 6 2
0 ⌈ a
┌→────┐
│0 6 2│
└~────┘

(Downstile)

monadic (Floor)

Rounds down to nearest whole number

⌊ 3.4 ¯3.4 3 0
┌→───────┐
│3 ¯4 3 0│
└~───────┘

dyadic (Minimum)

Takes minimum of 2 values

4 ⌊ 6 ⌊ 2.5
   
2.5