# Does any programming language use variables as they're in maths?

In maths, a variable means you can put any number there, and an equation will still be true:

``````root(square(x)) = abs(x)
``````

In programming languages, this is not so: a var can change. In Python:

``````y = (x**2)**.5
x *= 2
assert y == abs(x)
``````

will raise an exception, since x in the last line is not the same.

Are there programming languages that use immutable variables?

-
In mathematics, most equations are not identities. For instance, x^2 + 5 = 6x is valid if and only if x = 1 or x = 5. Equations that are valid for all values in some "universe" (such as the set of all real numbers), e.g. sin(2x) = 2 sin x cos x, are called identities. – Andreas Rejbrand Oct 16 '10 at 21:57
variables in math != variables in programming languages. I have always thought that they should have different names as well, since so many people are confused when they bring preconceptions about variable in math to programming. – Lie Ryan Oct 16 '10 at 22:54
I think that the goal of variables in math is not the same as in programming. In math, variables represent a unknown value that you want to know or a place into a formula that you puts a value to take a result. For common programming languages, only the second case make sense. For the first case you´ll need a software like Mathematica. – Pagotti Oct 17 '10 at 2:56

To answer your title question "Does any programming language use variables as they're in maths?": C, C#, Java, C++, and any other C style language use variables in the way they are used in math.

You just need to use == instead of =.

``````root(square(x)) = abs(x)
``````

Then I can translate that into C# directly without any changes other than for the syntax. `Math.Sqrt(Math.Pow(x,2)) == Math.Abs(x)`

This will evaluate to true for any value of x as long as x squared is less than the max for the data type you are using. (Java will be grossly similar, but I believe the Math namespace is a bit different)

This next bit will fail to compile in C# because the compiler is smart enough to know I can't assign the return of one operation to another operation.

`Math.Sqrt(Math.Pow(x,2)) = Math.Abs(x)`

Immutability has nothing to do with this. You still need to assign the value in an immutable language and it's entirely possible that a given language may chose to do this by using = as the operator.

Further proving the point, this loop will run until you exhaust legal values of x and get an overflow exception:

`````` while (Math.Sqrt(Math.Pow(x, 2)) == Math.Abs(x))
{
++x;
System.Console.WriteLine(x);
}
``````

This is why mathematicians hate the use of = for assignment. It confuses them. I think this has led you to confuse yourself. Take your example

``````y = (x**2)**.5
x *= 2
assert y == abs(x)
``````

When I turn this into algebra, I get this:

abs(2x) = root(x^2)

Which of course is not true for values other than 0. Immutability only saves you from the error of changing the value of x when you add extra steps between evaluating the Left Hand Side and Right Hand Side of the original equation. It's doesn't actually change how you evaluate the expression.

-
Thanks for this comprehensive explanation! – culebrón Oct 17 '10 at 11:24

Purely functional programming languages, such as Haskell, enforce immutable variables. I like to call them identifiers though, instead of variables.

-
In Erlang, which is an impure functional language, variables are immutable, too. – sepp2k Oct 16 '10 at 18:29
I don't know too much Erlang. Those are the atoms, right? Does Erlang allow any sort of mutable variable-like storage? – Ionuț G. Stan Oct 16 '10 at 18:34
@Ionut: No, Erlang atoms are what are called symbols in lisp and ruby. Regarding your second question: It has the process dictionary, which is a global (well, per-process), mutable hash map. However that is used very rarely in my experience. Other than that the only way to break referential transparency is message passing. – sepp2k Oct 16 '10 at 18:45
Hm, I should learn myself some Erlang :) – Ionuț G. Stan Oct 16 '10 at 18:49
how does this answer the question asked? – gnat Oct 7 '13 at 18:01

You can simulate immutability in Python too by not allowing updates to classes. As stated earlier most pure functional programming languages enforce immutability. Clojure is a recent addition to the JVM platform (Clojure is a LISP dialect)

In Scala (also a JVM language) a unifier of OO and FP support variables declared with `val` are immutable. Scala expressiveness and OO/FP hybrid support makes it look similar to Mathematica. (Akka is a framework that adds Erlang OTP and Clojure functionality such as STM.. to Scala and Java.)

In Java a variable declared `final` is immutable, and Java libraries such as Google Guava includes immutable libraries of collections. Java `String`s are also always immutable.

-

The `=` sign used in programming languages is misleading. `<-`, meaning "store", or even COBOL `MOVE` should be used instead.

As an addition, Prolog uses variable like math: Variables cannot change, and the Prolog engine will fill in the variables to see if the solutions exist.

Also, Curry is a mix of Haskell and Prolog.

-

C, C++, and Objective-C can specify immutability for function (and method) arguments, as well as other variables (identifiers), with the "const" type qualifier.

-