Learn APL with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
APL Counter and Theme Toggle
count ← 0
isDark ← 0
updateUI ← {
'Counter: ',⍕count
'Theme: ',(('Light','Dark')[isDark+1])
}
increment ← {
count ← count + 1
updateUI ⍬
}
decrement ← {
count ← count - 1
updateUI ⍬
}
reset ← {
count ← 0
updateUI ⍬
}
toggleTheme ← {
isDark ← 1 - isDark
updateUI ⍬
}
⎕ ← 'Simulate actions'
updateUI ⍬
increment ⍬
increment ⍬
toggleTheme ⍬
decrement ⍬
reset ⍬
Demonstrates a simple counter with theme toggling using APL variables and array-oriented expressions.
2
APL Fibonacci Sequence
fib ← 0 1
10 {fib ← fib , +/¯2↑fib} ⍬
fib
Generates first 10 Fibonacci numbers using APL array operations.
3
APL Factorial Calculator
n ← 5
×/⍳n
Calculates factorial of a number using APL product of integers.
4
APL Prime Checker
n ← 13
(2≤⍳n-1) / n|⍳n-1 ⍝ returns 0 for prime
Checks if a number is prime using modulo and all.
9
APL Simple Alarm Simulation
temp ← 80
thresh ← 75
(temp>thresh)/'Alarm: Temp Too High!'
Simulates an alarm if threshold exceeded.
10
APL Random Walk Simulation
steps ← 10
pos ← 0
{pos ← pos + 2x?2-1}⍣steps ⍬
Simulates a 1D random walk using APL random numbers.