Substitution: replace a name with a value

LLM-authored, human-reviewed

Foundations

“Evaluate 2x+12x + 1 at x=3x = 3.” You learned this in first-year algebra. It is the basic move behind nearly everything on this site. The name is substitution. Functions, beta-reduction, and much of programming use the same operation. This article gives you the short version for the substitution exercises, then points to the fuller treatment in algebra you forgot.

A variable is a placeholder, not a box

Start with what a variable is. In algebra, a variable such as xx is a name that stands for a value. The expression 2x+12x + 1 is a template, not a statement about one particular xx. It means “double whatever this name stands for, then add one.” A variable is a hole to fill. It is not a storage location that changes later.

That differs from how most programming languages describe a variable. In JavaScript, let x = 3 puts a value in a box that you can reassign. In algebra - and in Elixir - a name is a pure stand-in: the same name in two places in one expression refers to the same value in both places. Reading it again does not produce a new value.

Substitution is the fill-the-hole move

Substitution replaces a variable with a value (or another expression) everywhere it appears. To evaluate 2x+12x + 1 at x=3x = 3, replace xx with 33:

2x+123+172x + 1 \to 2 \cdot 3 + 1 \to 7

That is the operation. It has three steps, and each one is a common source of mistakes:

  1. Replace every occurrence. If the name appears twice, you replace it twice. x2+xx^2 + x at x=4x = 4 becomes 44+44 \cdot 4 + 4, not 4x+44 \cdot x + 4.
  2. Substitute the whole value. When the value is negative, wrap it in parentheses so the sign travels with it. x2+2x1-x^2 + 2x - 1 at x=3x = -3 is (3)(3)+2(3)1-(-3) \cdot (-3) + 2 \cdot (-3) - 1, which is 961=16-9 - 6 - 1 = -16. Compute the sign carefully - the leading minus applies to the whole product, not to one factor alone, and that is exactly where the substitution exercises plant their traps.
  3. Do it simultaneously when there are several names. Substituting x=3x = 3 and y=2y = -2 into xyx+yxy - x + y means both replacements happen at once, on the original expression: 3(2)3+(2)=113 \cdot (-2) - 3 + (-2) = -11. You do not substitute xx, simplify, then substitute yy.

It is the same move as calling a function

Calling a function performs substitution. The function is a template; the call fills its hole:

function f(x) { return x * x + 1; }
f(3);   // substitute 3 for x, then evaluate: 3 * 3 + 1 = 10

The parameter x is the variable, and the call f(3) is the substitution. Programming copied this idea from algebra. Elixir makes the connection explicit: f(3) and “evaluate the template at x=3x = 3” are the same idea. In Elixir, x = x + 1 does not increment a box. It rebinds the name to a new value while the old one disappears.

Why this matters here

The substitutions exercises practice this one move until it is fluent. The evaluate and substitute_expression exercises ask for one thing - “substitute this value into this expression and give the number” - because the move is the lesson, not the surrounding arithmetic. The exercise set treats a variable as a placeholder you fill in, never as “the unknown you solve for.” The harder exercises make the three slip points above (every occurrence, negative signs, simultaneous substitution) automatic.

This deserves an exercise set because substitution is the operation a formal system can be built from. It also leads into lambda calculus. Lambda calculus has exactly one rule, beta-reduction: apply a function to an argument, substitute the argument for the variable throughout the body, repeat. If “evaluate x2+1x^2 + 1 at x=3x = 3” feels automatic, beta-reduction is the same move in different notation. If it does not, use this practice until it is automatic.

Where to go next

← Back to articles