Examples

Copyable examples for every version of epher. Paste them into a terminal, a REPL session, or the app's entry field. The user guide explains the language in detail; this page is for quick copying and pasting.

On a phone, tap an example to open the app with it ready to run.

TUI, desktop app, and web app

The same language runs in the terminal UI, the desktop app, and the web app. Copy/paste an example into the entry field and press Enter.

A straightforward calculation. Multiplication happens before addition.
2 + 3 * 4
Powers and roots work like the rest of the language.
sqrt(2 ^ 10)
Exact fractions where binary floats would round.
frac(1, 3) + frac(1, 6)
Defining a function and calling it in the same line.
def sq(x) = x * x; sq(9)
A function with steps: a `do ... end` body runs its statements in order and answers with the last one.
def hyp(a, b) do
  c = a ^ 2 + b ^ 2
  sqrt(c)
end
hyp(3, 4)
`return` answers early: the loop stops the moment the search finds.
def firstsq(xs) do
  for x in xs do
    if x ^ 0.5 == floor(x ^ 0.5) then return x
  0
end
firstsq({3, 5, 9, 11})
Split a text, name the pieces, and join them back: the string library at work.
{name, ext} = split("report.txt", ".")
join({upper(name), ext}, ".")
Arithmetic across number bases: `0xff` is hex, `0b1` is binary, and `hex(...)` spells the answer as hex.
0xff + 0b1
hex(ans)
A multi-line script. Shift+Enter starts a new line, Enter runs the whole script as one history item, and every answer shows in order.
x = 10
y = x + 5
y ^ 2
A basic 2D curve.
graph x ^ 2
The region below the curve, shaded. `y >` shades above instead.
graph y < x ^ 2
The play button beside `a` animates the wave.
const a = 1
graph sin(x * a)
Two curves on one plot. Every `graph` line adds another.
graph x ^ 2
graph sin(x)
A 3D surface. Drag, swipe, or use the sliders to orbit.
graph3d sin(x) * cos(y)
A paraboloid bowl.
graph3d x ^ 2 + y ^ 2
Kepler's equation: the eccentric anomaly E for a mean anomaly of 5 degrees and an eccentricity of 0.1.
kepler(5, 0.1)
Planck's blackbody curve for the Sun (5778 K), wavelength in nanometres. The constants `h`, `c` and `k_b` are built in.
def planck(x) = (2 * h * c ^ 2) / ((x * 1e-9) ^ 5 * (exp((h * c) / ((x * 1e-9) * k_b * 5778)) - 1))
graph planck(x) from 100 to 2000
The solar system at an instant: orbits, trails, and labelled dots. Give the time as a constant and press play to watch the planets move.
const t = now()
solar3d t
A transit light curve: a smooth 1.2 percent dip as an exoplanet crosses its star, modelled with a logistic ingress.
def flux(x) = 1 - 0.012 / (1 + exp((abs(x) - 0.04) * 200))
graph flux(x) from -0.2 to 0.2

The command line

One-shot calculations, piped scripts, and SVG exports. Copy an example from those below into any shell.

Piping a script into the epher command. stdin becomes the script, one line at a time.
printf 'x = 10\nx ^ 2\n' | epher -
Piping epher's output into another command.
epher "1 / 3" | tee third.txt
Several statements on one line, separated by semicolons.
epher "x = 7; x ^ 3"
A recursive factorial, defined inside a piped script.
printf 'def fact(n) = if n <= 1 then 1 else n * fact(n - 1); fact(10)' | epher -
A 2D graph saved as an SVG image file.
epher "graph x ^ 2; graph save plot.svg"
A 3D surface saved as an SVG image file.
epher "graph3d x ^ 2 - y ^ 2; graph3d save saddle.svg"

The REPL

Start an interactive session with `epher repl` and type after the `epher>` prompt. These blocks run one after another in the same session. Each answer feeds the next line's `ans`.

The first calculation of the session.
epher> 2 + 3
= 5
`ans` holds the previous answer. Build on it.
epher> ans * 10
= 50
`ans` follows every new answer down the session.
epher> ans + 2
= 52
Use `ans` inside a larger expression.
epher> (ans - 12) ^ 2
= 1600