</> APL Run Loading interactive WebAssembly workspace…

🎓 Chapter 8: Searching, Sorting & Slicing

APL provides high-level primitives for searching, indexing, sorting, and structural manipulation.

---

1. Searching & Lookup (⍳, ∊, ⍷)

  • Membership (X ∊ Y): Returns a boolean array testing if elements of X exist in Y.
  • Dyadic Index Of (Y ⍳ X): Finds the first 1-based index location of each item of X inside vector Y.
⍝ Test membership of numbers 1..10 in prime list
(⍳10) ∊ 2 3 5 7

---

2. Sorting with Grade Up (⍋) and Grade Down (⍒)

Rather than mutating arrays directly, APL sorting uses Grade Up (⍋ Y) or Grade Down (⍒ Y) to generate a permutation vector of indices that orders Y.

To sort Y, index back into Y using its grade: Y[⍋ Y]!

Data ← 42 17 89 3 55
⍝ Sort Data in ascending order
Data[⍋ Data]

---

3. Structural Slicing (↑, ↓)

  • Take (N ↑ Y): Takes the first N elements (or last N if negative ¯N).
  • Drop (N ↓ Y): Drops the first N elements.
⍝ Take top 3 elements & drop first 2 elements
(3 ↑ Data) ⋄ (2 ↓ Data)

← Chapter 7: Each (¨) & The Rank Operator (⍤) Chapter 9: Matrix Algebra, Transpose (⍉) & Linear Solvers (⌹) →