K9 Public Docs

APL: A Practical Introduction

Type-forward, information-dense APL basics: core ideas, glyphs, examples, outputs.

What APL Is

APL is an array language: operators work on scalars and arrays without explicit loops. You describe whole transformations, not steps.

Reading is right-to-left: a function applies to the argument on its right. After a few hours, this feels direct and mathematical.

APL in one line: think in arrays, write in operators.

Arrays First (Core Idea)

APL treats every value as a scalar or array. That single idea drives most of its power.

⍳5           ⍝ 1 2 3 4 5
3⍴1          ⍝ 1 1 1
(1 2 3)+10   ⍝ 11 12 13
+/1 2 3 4    ⍝ 10

No loops. The operators already know how to work element‑wise or reduce across an array.


Glyphs at a Glance (Most Used)

Iota
Generate 1…n
Rho
Shape / reshape
+
Add
Element-wise add
Subtract
Element-wise subtract
×
Multiply
Element-wise multiply
÷
Divide
Element-wise divide
Reverse
Reverse array
Take
Take first n
Drop
Drop first n
/
Reduce
Fold with function
\
Scan
Prefix reduction
Find
Pattern search
Enclose
Box an array
Disclose
Unbox / pick
=
Equal
Comparison
Not equal
Comparison

Full Glyph Reference (Print-Ready)

Glyph Name Use Example → Output
+AddElement-wise add1 2 3 + 10 → 11 12 13
SubtractElement-wise subtract10 − 3 → 7
×MultiplyElement-wise multiply3 × 4 → 12
÷DivideElement-wise divide10 ÷ 4 → 2.5
*PowerExponentiation2 * 3 → 8
|AbsoluteMagnitude|¯5 → 5
FloorRound down⌊3.7 → 3
CeilingRound up⌈3.2 → 4
=EqualComparison3 = 3 → 1
Not equalComparison3 ≠ 4 → 1
<Less thanComparison3 < 5 → 1
>Greater thanComparison5 > 3 → 1
Less or equalComparison3 ≤ 3 → 1
Greater or equalComparison5 ≥ 5 → 1
MatchExact match1 2 3 ≡ 1 2 3 → 1
Not matchNot exact match1 2 3 ≢ 1 2 4 → 1
IotaGenerate 1…n⍳5 → 1 2 3 4 5
RhoShape / reshape3⍴1 → 1 1 1
,RavelFlatten,1 2 3 → 1 2 3
ReverseReverse array⌽1 2 3 → 3 2 1
TakeTake first n2↑⍳5 → 1 2
DropDrop first n2↓⍳5 → 3 4 5
/ReduceFold with function+/1 2 3 4 → 10
\ScanPrefix reduction+\1 2 3 → 1 3 6
FindPattern search'ab' ⍷ 'abc' → 1 0 0
EncloseBox array1 0 1 0 1 ⊂ 'abcde' → [ab] [cd] [e]
DisclosePick from box1 ⊃ 'hello' → h
FormatTo string⍕123 → 123
⎕UCSUnicodeCode ↔ char⎕UCS 'A' → 65
⎕JSONJSONEncode/Decode⎕JSON 1 2 3 → [1,2,3]

This table is designed to print cleanly. Use browser print to PDF.


Keyboard + Input (Compact Guide)

APL is fastest with a real APL keyboard layout. Install one, print the keymap, and learn the modifier layer.

Platform Fast Path Fallback
macOS Add an APL input source (Dyalog/Unicode). Toggle with Globe or Control+Space. Unicode Hex Input for one‑offs; copy/paste glyphs from this page.
Windows Install an APL keyboard layout; use AltGr layer for glyphs. IME/clipboard glyph palette or compose via custom layout.
Linux IBus/SCIM/XKB APL layout or a Compose key map. Compose sequences or paste from a glyph table.
APL keymap glyph layer (representative)
Representative APL glyph layer (positions vary by layout). Open SVG.

Tip: keep a printed keymap near your keyboard for the first week. Muscle memory appears quickly.


Examples + Outputs

Rolling totals
+\1 2 3 4
Output
1 3 6 10
Filter by mask
v ← 10 20 30 40 50 (v ≥ 30) / v
Output
30 40 50
Reshape
2 3⍴⍳6
Output
1 2 3 4 5 6
Reverse + take
3↑⌽⍳7
Output
7 6 5
Match
1 2 3 ≡ 1 2 3
Output
1

Strings + JSON Edge Cases

Find substring
'hello' ⍷ 'hello world'
Output
1 0 0 0 0 0 0 0 0 0 0
Pick character
1 ⊃ 'hello'
Output
h
Partition
1 0 1 0 1 ⊂ 'abcde'
Output
[ab] [cd] [e]
JSON encode string
⎕JSON 'hi'
Output
"hi"
JSON decode string
0 ⎕JSON '"hi"'
Output
hi

Python / NumPy → APL

Intent Python / NumPy APL Notes
Range 1..n np.arange(1, n+1) ⍳n APL defaults to 1-based
Element-wise add a + b a + b Same operator
Reshape a.reshape(2,3) 2 3⍴a Shape is left argument
Sum a.sum() +/a Reduce with +
Cumulative sum np.cumsum(a) +\a Scan with +
Reverse a[::-1] ⌽a Single glyph
Take / head a[:n] n↑a n on the left
Drop / tail a[n:] n↓a n on the left
Boolean filter a[a > 0] (a > 0) / a Mask used as filter
Find substring "ab" in s 'ab' ⍷ s Returns a boolean mask

K9-Specific Examples

Outputs shown match K9 test coverage.

Unicode codepoint
⎕UCS 'A'
Output
65
Codepoints to string
⎕UCS 65 66 67
Output
ABC
JSON encode
⎕JSON 1 2 3
Output
[1,2,3]
JSON decode
0 ⎕JSON '[1,2,3]'
Output
1 2 3
Trap + continue
⎕TRAP←(3 'C' '⎕DMX') ⋄ ⎕UCS ¯1
Output
3

APL One‑Page Cheat Sheet (Print‑Ready)

Optimized for a single printed page. Use browser print to PDF or open the standalone cheat sheet: cheat-sheet.html.

Array Basics

⍳nGenerate 1…n
⍴AShape of A
m n⍴AReshape A
,ARavel (flatten)
A,BCatenate

Structural

n↑ATake
n↓ADrop
⌽AReverse
⊂AEnclose
⊃ADisclose / pick

Arithmetic

A+BElement‑wise add
A×BElement‑wise multiply
A÷BElement‑wise divide
|AAbsolute value
⌈A / ⌊ACeiling / floor

Reductions

+/ASum
×/AProduct
⌈/AMax
⌊/AMin
+\APrefix sums

Comparisons

A=BEqual
A≠BNot equal
A<B / A>BLess / greater
A≤B / A≥BLess‑eq / greater‑eq
A≡BMatch

Strings + I/O

S ⍷ TFind substring
⍕AFormat to string
⎕UCS AUnicode codepoints
⎕JSON AEncode JSON
0 ⎕JSON SDecode JSON

Idioms

(A>0)/AFilter by mask
⌽⍳nReverse range
2 3⍴⍳6Matrix
+/AReduce with +
+\AScan with +

Getting Started

This intro is compact on purpose. It’s a base for deeper sections on functions, tacit programming, namespaces, and performance.