-2

A common thing in programming, a good example being JavaScript, is using ternary if statements to assign values to a variable.

For example:

const speed = time === 0 ? undefined : (distance / time);

Often times you can shorten code such as

if (condition) {
  f('test', 5);
} else {
  f('test', 6);
}

to just

f('test', condition ? 5 : 6);

by moving the conditional inside the function.

This seems pretty similar to something I remember from learning Calculus in college where you could move a $\lim$ inside if $f$ was continuous at some point.

From https://math.stackexchange.com/a/1519038/761845:

if function $f$ is continuous at $g$ and $\lim\limits_{x\to x_0} g(x)=g$ then:

$$\lim_{x\to x_0}f(g(x))=f\left(\lim_{x\to x_0} g(x)\right)$$

Is this just coincidence that these are similar looking or are the same mathematics at work?

If so, what are those mathematics?

Alex D
  • 147
  • It is similar only intuitively, I think. It's not the same. The programming example you provide... it's more about things like: f(g(h(x))) = g(f(h(x))) e.g. But there's no notion of limit in programming. In fact most of the calculus does not fit good into programming. It's discrete math which is more closely related and mostly used in programming. – peter.petrov Apr 09 '21 at 15:43
  • Not really. Under the hood, the ternary construct turns out the same code (up to optimization) as the structural if. There is no actual commutating taking place; this is closely similar to var temp = g(x); return f(temp) versus return f(g(x)). Thus the ternary contruct works for arbitrary $f$, the limit swapping works only under continuity assumptions. – Hagen von Eitzen Apr 09 '21 at 15:44
  • 1
    Perhaps it's more related to currying – Dan Rust Apr 09 '21 at 15:44
  • I was sort of thinking of it as how booleans are lambda encoded as functions, so I guess it's sort of like moving the bool encoding inside. I don't know if that "moving the function inside" has the same sort of semantics as moving a lim inside, since I don't know if lim behaves the same semantically as a higher order function – Alex D Apr 09 '21 at 16:39

1 Answers1

1

It looks similar but not the same. Consider the order of operations here: $$ \texttt{f("test"${}^1$, condition${}^2$ ?${}^3$ 5${}^4$ : 6${}^4$)${}^5$} $$ We will process the string "test" first, then calculate the condition then check if it is true or false, then process either 5 or 6 and then take the function $f$ of the calculated arguments. If you analyze what happens with if example, the order of operations will be the same. So what you have is an equivalent syntax (syntax sugar if you want) rather than a commutation of operations (like limit and some functions). A proper mathematical example of this is the precedence of operations: $$ b×c+a = a+b×c $$ The syntactic order of the symbols is different, but due to the precedence of $\times$ and $+$, both mean exactly the same.

Vasily Mitch
  • 10,129
  • Thanks. Would it be different if this were in lambda calculus? If you lambda encode condition as a boolean where true = \x.\y.x and false = \x.\y.y and condition = true or false, then you have this written as condition(f('test', 5))(f('test', 6)) vs f('test', condition(5)(6)). These seem different to me, as one has condition's \x variable taking whatever f returns, and in the other case \x is numeric. It seems like there would be a mathematical rule for that, or is it still essentially just "syntatic sugar"? – Alex D Apr 09 '21 at 21:19