DragginMath: Named Expressions

You may sometimes want to use the same expression over and over, but you would rather not have to type it over and over. This is what a Named Expression does.

To create a Named Expression, write an expression the way you normally would, then end it with ≕ and an uppercase letter, for example √(x↑2+y↑2)P. You can choose any uppercase letter from A to Z as a name. Note that ≕ is a single character with its own key on the screen keyboard. This operator is pronounced assign, and it has history (see below). It is DragginMath’s cue to associate this expression with that name. You can do this with top-level expressions, or with subexpressions. If you do it with subexpressions, they must be in parentheses. Your new Named Expression is remembered as soon as you type it, even if it is in a subexpression. This allows you to reuse a Named Expression in the same expression that defines it.

To reuse a Named Expression, type its name, for example P. The Named Expression’s value is written into the current expression just as if you had typed it there.

You can also reuse a Named Expression by double-tapping it in the Named Expression dialog list. In this dialog, you can review the current definitions if you have forgotten what they are.

If you type a name that has no definition, DragginMath lets you know that won’t work by flashing red in its usual fashion.

Named Expressions are remembered until you either delete their definitions or delete the DragginMath app entirely. To delete a definition, swipe left on it in the Named Expression list, then confirm its deletion.

These definitions can do more than you might expect. For example, enter a(b+c)↑2D. At this point, the value of D is a(b+c)↑2. Double-tap ∗ in the operator tree (evaluate it), then double-tap ≕. The value of D is now a(bb)+(a(bc)+a(cb))+a(cc). Yes, ≕ is an actual operator that actually does something. So you can enter an expression in one form, turn it into the equivalent thing that you really want, then double-tap ≕ to remember it that way without having to type it again.

The ability to replace a short easy-to-type name with some longer string of characters is known to programmers as macro expansion. While the basic idea is simple, there are several ways one might design such a thing. The various designs are powerful in different ways, but are also often fraught with complications and obscure gotchas.

DragginMath’s design must be easy and safe for non-professionals to use. This results in three restrictions most people won’t notice, but ambitious users may find limiting. If you are certain you will never be an ambitious user of DragginMath, you may skip the rest of this section.

0) When your Named Expression is written into the current expression, it is automatically as if it were in parentheses. This ensures you don’t create unintended precedence interactions with other operators around it.

1) Named Expression definitions are remembered as definitions in Expression History, but not in the value of a Named Expression definition itself. This means definitions embedded within definitions are remembered only as their value. For example, if you type (2+3A)+4B, this appears without change in Expression History, but the value of A is now 2+3 and B is 2+3+4. In the definition of B, the embedded definition of A is removed and replaced with only its value.

2) Named Expression references by uppercase letter are always replaced immediately as you type. For example, if you type 1+B+5C, that appears as 1+2+3+4+5C in Expression History, and the defined value of C is 1+2+3+4+5. Replacement inside a definition happens when the definition is made, not when the definition is used. There is intentionally no way to make replacement happen later.

There are useful things you could do if these restrictions didn’t exist, but removing them enables risks ranging from confusing to fatal. These risks are impossible to control without either a more sophisticated language, a more sophisticated user, or both.

If a Named Expression has been defined, you can access its value in any expression containing a similarly named variable. This is called Value Replacement. For example, if you define 7X, then any occurrence of the variable x in any expression can be replaced with the value 7 by double-tapping x. This does not happen automatically when you evaluate an expression: you must double-tap each variable node deliberately.

If you have a hardware keyboard attached to your iPad, you may enter a colon : for assign, which converts to ≕ automatically.

Would you like to know where ≕ came from and why we use it here?

One of the earliest, most important, and most forgotten programming languages is Algol. If you ever study the history of Computer Science, be sure to learn Algol and how it differed from Fortran, its far-dominant competitor. In the lineage of programming languages, so many descend from Algol, including some you probably have heard of. A lot of serious software was written in Fortran, but it had little impact on subsequent language design except for one thing: its assignment operator =, which most languages have chosen to use.

Unlike most languages, the symbolic design of Algol does not pretend that assignment is the same idea as equality. Therefore, unlike most languages, Algol does not use = as its assignment operator. Instead, it uses := (two characters together, both found on any standard keyboard). The evaluated expression is on the right and the assigned variable on the left, as in p := sqrt(x*x+y*y). The right-to-left flow of assignment is how it works in almost all programming languages, and whether or not that is a good thing is debatable. But in computing environments that are more about interacting with things than writing programs, it definitely makes more sense for assignment to work the other way, from left to right. A calculator is one such environment. DragginMath is another.

DragginMath uses the industry-standard Unicode character set to represent the characters it draws on the screen. Along with letters and punctuation from many natural languages, Unicode also has mathematical and computing characters. One of these is the single character Colon-Equals ≔ resembling the two-character Algol assignment operator :=. It also has an Equals-Colon ≕ character that means assigning from left to right. This is what we use in DragginMath.