F1 · Algebra · Lab
Functions on the plane
A function is a curve. Two functions are two curves — and composing them, applying one then the other, draws a third. This lab lets you plot, compose, and trace a point through f∘g.
Everything F1 built comes together here on a coordinate grid. Sampling a function across a range and collecting the outputs is F1.07’s map; chaining two functions is F1.03’s composition; and because order matters, f∘g and g∘f are usually different curves. Pick two functions, choose what to overlay, and slide the trace to watch a single value make its way through the pipeline.
Plot, compose, trace
A curve is the set of points (x, f(x)). To draw it, sample x
across a range, apply f to each, and join the results — exactly
Enum.map(xs, f). The composite f∘g sends each
x through g first, then f, so
its curve is the map of fn x -> f.(g.(x)) end.
Choose f and g, toggle the overlays, and slide the trace.
- plane
- the coordinate grid where every point is a pair (x, y).
- curve
- the points (x, f(x)) a function traces.
- composite
- f∘g: apply g, then f.
- sample
- the finite set of x values mapped to draw a curve.
The plotter · f, g, and their composites
Algebra
The curve of f∘g is each x sent through g then f.
Elixir
Enum.map(xs, fn x -> f.(g.(x)) end) — one image per sample, a new list of points.
Plotting is mapping a function across a range; composing is chaining two before you map. Toggle g∘f on and the curves part ways — the order of composition is visible as two different shapes.
Closing the chapter
This lab gathers the whole of F1. A function is a mapping from inputs to outputs (F1.01); evaluating it is substitution (F1.02); chaining two is composition, and order matters (F1.03); the values it works on do not change underneath it (F1.04); a range of inputs is a collection (F1.05); drawing the curve point by point is recursion, or the map that packages it (F1.06, F1.07); and choosing a function from a palette is a match on a name (F1.08). Seen on the plane, f∘g is one curve, drawn by mapping the composite across a sample — algebra and code describing the same picture.
F1 · Algebra is complete. F2 · Functional Programming takes these ideas off the page and makes them the working shape of Elixir: pure functions, persistent data, closures, and folds.
Next chapter: F2 · Functional Programming — purity, persistent data, higher-order functions, and folds as the everyday tools of the language.