Examples of every construct
The language construct by construct. You can run and edit every snippet on these pages, and a test checks that each one still prints the output shown: the docs and the tests are the same 157 examples.
Numbers & arithmetic
Numbers are 64-bit floats, Int is a 64-bit integer, and Bool holds truth values.
16 examplesBooleans
true and false are their own type: what a comparison yields, what if and while take, and a value you can store, pass and return.
8 examplesControl flow
if/else, while, the counted for loop, and the loop-control statements.
11 examplesFunctions
def, return, docstrings, recursion, and optional type annotations.
10 examplesArrays
Literals, zeros, indexing, elementwise arithmetic, and passing arrays through functions.
17 examplesComplex numbers
A complex number type: the `i` suffix on a number makes it imaginary, arithmetic mixes complex and real values, and every engine (the interpreter, C and wasm) computes the complex functions with the same formulas, so they print the same digits.
4 examplesComplex arrays & the FFT
Packed complex arrays, and a Fourier transform that works at every length and gives identical output in every engine.
8 examplesLinear algebra
The matrix product, transpose, and the symmetric eigensolver.
9 examplesLambdas & higher-order builtins
fn values, quadrature, and exact differentiation.
13 examplesScope & mutation
Where names live, what functions can see, and which values are shared.
6 examplesProse in programs
Comments, docstrings, narration, and annotation markers.
5 examplesPlotting
plot prints a structured record, and the page draws the chart from it.
7 examplesAnimation
An animation is a Viv program: it creates named objects, and transitions reveal, morph and move them. The program prints records; the page plays them back with a scrubber.
4 examplesTesting
test blocks and the assert family: a program checks its own results and prints which tests passed and which failed.
3 examplesErrors, exactly as the compiler says them
The error messages are part of the language. Each one below is what the compiler prints, and the site's tests fail if a message changes.
16 examplesGotchas
Float facts and Viv-specific surprises, each shown by a program that runs.
6 examplesStencils and PDE steps
A finite-difference stencil is arithmetic on shifted copies of an array, and an implicit time step is a tridiagonal solve. These are the pieces the PDE solvers are built from.
7 examplesSmall algorithms
Classic numerical algorithms, each a few lines long.
7 examplesTwo topics to read first
Errors — every error message on that page is the compiler's actual message, checked by the doctest suite, so a change to any of them fails a test.
Scope & mutation — where names live and which
values are shared. Assignment and parameter passing alias arrays, so
b = a shares storage and a function that writes into an array parameter writes
into the caller's array. It is the rule most likely to surprise you if you guess.
Python and Viv, side by side — small programs written in both languages, with the same output, lined up region by region, for a reader who knows Python.