R3 — Naming things
Goal: give names to values and to formulas, understand why the two behave differently, and learn the command that gets you out of trouble.
Assignment is :=
> a := 3
a := 3.0
> a + 1
4.0
The echo repeats what was stored, which is worth reading rather than skipping — later in this lesson it is how you tell a value from a formula.
Why not =?
Because = already means something, and it is not assignment:
> a = 4
false
That is an equation, and Leonardo answered it: a is 3, so a = 4 is false. An equation is a thing you can state, test and solve, which is what makes solve(x^2 = 4, x) meaningful in R5. Had = been assignment, none of that would be sayable.
Two symbols, two jobs: := stores, = relates.
A formula keeps its variables
Assign something containing a free variable and you get a definition rather than a value:
> g := sin(t) + t
g := (sin(t) + t)
> g
(sin(t) + t)
Look at the echo: it repeated the formula, where a := 3 repeated a number. Nothing was computed, because t has no value and there was nothing to compute. Give t one and g follows:
> t := 0
t := 0.0
> g
0.0
Definitions are late-bound, and that is the point
A definition is re-read every time you use it, so redefining what it depends on updates it — without touching the definition:
> h := 2 * g
h := (2.0 * g)
> h
0.0
> t := 1
t := 1.0
> h
3.68294
h means “twice whatever g is”, and g means “sin(t) + t for whatever t is”. Change t, and both move. That is how you build a model up in layers and then explore it by changing one number at the bottom.
When you want the opposite, freeze it
Sometimes a name should record a result rather than a relationship. consolidate evaluates now and stores the answer, so later changes cannot reach it:
> frozen := consolidate(2 * g)
frozen := 3.68294
> t := 2
t := 2.0
> frozen
3.68294
> h
5.81859
h moved with t; frozen did not. Use consolidate for a measurement you have already taken, and plain := for a relationship you want kept live.
Seeing what you have
> env
precision = 5
a := 3.0
frozen := 3.68294
t := 2.0
g := (sin(t) + t)
h := (2.0 * g)
env lists the settings as well as the names — values first, then definitions — so it answers both “what did I call that?” and “what state am I in?”.
unset — the command that gets you unstuck
> unset t
t unset
> g
(sin(t) + t)
With t gone, g is a formula again.
This is the most useful command in the lesson, because the commonest confusion at this prompt is an answer that comes back as a number when you expected algebra. The cause is almost always a name still bound from earlier. Two things bind without your typing :=:
- a
:=you have forgotten about, whichenvwill show you; solve, which stores its own answer — see R5, where it earns its own section.
When something reads oddly, env then unset is the fix.
Names the language has taken
> e := 3
cannot assign to 'e': it is a built-in constant
> simplify := 1
cannot assign to 'simplify': it is a reserved word
Constants and command words are refused rather than silently shadowed — otherwise the binding would be unreachable, since e always parses as the constant and simplify always starts a command.
Try it yourself
Define area as the area of a circle of radius r, evaluate it at r = 2, then make area symbolic again.
Answers
```text > area := pi * r^2 area := (3.14159 * (r ^ 2.0)) > r := 2 r := 2.0 > area 12.56637 > unset r r unset > area (3.14159 * (r ^ 2.0)) ``` *[run this session](https://lycogrypho.github.io/Leonardo/app/#s=precision%205%0Acolors%20dark%0Apretty%20off%0Alatex%20off%0Anames%20off%0Alogic%20minmax%0Alogic%20symmetric%20off%0Aexact%20precision%2030%0Aexact%20off%0Aa%20%3A%3D%203.0%0Afrozen%20%3A%3D%203.682941969615793%0Aarea%20%3A%3D%20(3.141592653589793%20*%20(r%20%5E%202.0))%0Ag%20%3A%3D%20(sin(t)%20%2B%20t)%0Ah%20%3A%3D%20(2.0%20*%20g))* Note the first echo: `area` stored the *formula*, with `pi` already folded to a number because that much was computable.What you learned
:=stores,=relates. An equation is a value you can test and solve.- A right-hand side with free variables becomes a late-bound definition that tracks its dependencies;
consolidatefreezes the current answer instead. - The echo tells you which you got: a number or a formula.
envshows bindings and settings;unsetis what you reach for when an answer is unexpectedly numeric.
Previous: R2 — Expressions and free variables · Next: R4 — Calculus · All lessons