μ
a minimal array language
Sinc surface: sin(r)/r with Bessel-like interference rings, computed on a 32×32 grid and rendered in-browser via WebAssembly.
What is μ?
μ is a minimal array language in the tradition of APL and K. It operates on scalars and homogeneous lists using single-character verbs and higher-order adverbs. Expressions are evaluated right-to-left. The interpreter is a ~1 kloc C program with no dynamic memory allocation.
Values & types
| type | example | notes |
|---|---|---|
long | 42 | 64-bit signed integer |
float | 3.14 | IEEE 754 double |
char | 'a' | single character literal |
symbol | `hello | interned string atom |
list | 1 2 3 | heterogeneous sequence |
func | {x+1} | lambda or verb alias |
Quick examples
Arithmetic is element-wise over lists:
1 2 3 + 10 20 30 // 11 22 33
2 * !5 // 0 2 4 6 8
Adverbs fold (/) and scan (\) reduce a list:
+/ 1 2 3 4 5 // 15
+\ 1 2 3 4 5 // 1 3 6 10 15
Lambdas use {expr} with implicit argument x:
f:{x*x}
f 1 2 3 4 // 1 4 9 16
Named arguments with explicit arg list:
dot:{[x;y] +/x*y}
dot 1 2 3; 4 5 6 // 32
Named math keywords work element-wise:
sqrt 1 4 9 16 // 1.0 2.0 3.0 4.0
abs -3 // 3.0
Vectors with multiple elements use bracket notation:
dot:{[x;y] +/x*y}
dot[1 2 3;4 5 6] // 32
Plot data (CLI saves SVG, playground renders to canvas):
plot 1 4 9 16 25 // simple plot (implicit x = 0,1,2...)
plot[1 2 3 4 5;1 4 9 16 25] // X vs Y plot with explicit x values
// Layered charts: compose layers into one chart
chart[line[x; y]; band[x; lo; hi]; hline 5; `Title]
A taste of what μ can do
Z-score a data vector in one expression:
{(x - mean x) % stddev x} 2 4 4 4 5 5 7 9
// -1.5 -0.5 -0.5 -0.5 0.0 0.0 1.0 2.0
Dot product and cosine similarity — no loops:
a: 3.0 1.0 4.0
b: 1.0 5.0 9.0
(+/ a*b) % ((sqrt +/a*a) * (sqrt +/b*b))
// 0.834
Newton's method for √2 — iterate 8 times, or converge automatically:
8 {[v] 0.5*(v + 2.0%v)}/ 1.0 // iterate 8 times
// 1.4142135623731
{[v] 0.5*(v + 2.0%v)}/ 1.0 // converge until stable
// 1.4142135623731
Load and analyse a CSV file:
t: csv "data.csv" // parse CSV → (headers; columns)
t@0 // `ticker `price `volume `change `sector
mean (t@1)@1 // average of the price column
Black-Scholes European call (closed form, defined in examples):
bscall 100 100 0.05 0.2 1.0
// 10.450 (exact: 10.4506)
Markowitz tangency portfolio then Kelly-size it — three lines with linalg:
S: covmat[cols] // covariance matrix from return columns
w: mkw[S; mu; rf] // max-Sharpe weights (sum to 1)
kelly[portret[w;mu]; portvar[w;S]; rf] // full-Kelly leverage fraction
Fit a vol smile to market implied variances using SVI parametrization:
ks: (-0.4),(-0.2),0.0,0.2,0.4 // log-moneyness grid
market_vols: 0.24,0.21,0.19,0.21,0.24
market_ws: market_vols * market_vols
params: (svi_fit_auto[ks; market_ws]) // [a; b; ρ; m; σ]
fitted_vols: sqrt each[{svi_w[params;x]}; ks]
OLS linear regression with in- and out-of-sample prediction:
xs: 1.0,2.0,3.0,4.0,5.0
ys: 2.3,3.8,5.9,8.1,9.7
m: (slope[xs;ys]) // → 1.855
b: (intercept[xs;ys]) // → 0.37
r2: (rsq[xs;ys]) // → 0.9989
preds: (predict[xs;ys; 6.0,7.0]) // → 11.5 13.35
See the examples page for formulas, derivations, and the full Monte Carlo suite. Interactive worked examples — vol smile calibration, linear regression, portfolio construction — are on the notes page.
Try it now
Open the μ playground — a fully functional μ interpreter running in your browser via WebAssembly. No installation required, with pre-loaded examples and session persistence.
Getting started
Build
git clone https://github.com/martinsk/mu
cd mu
make
Run the REPL
./mu
μ> +/ 1 2 3 4 5
15
Run a script
./mu myscript.mu
Load a file mid-session
μ> \l stdlib/stats.mu
Run tests
make test-san
See grammar for the full language reference, stdlib, stats, and linalg for the standard library, and CONTRIBUTING.md for coding conventions.