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 RejbrandOct 16 '10 at 21:57

4

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 RyanOct 16 '10 at 22:54

1

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.
–
PagottiOct 17 '10 at 2:56

5 Answers
5

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 =.

If I take your original

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:

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.

In Erlang, which is an impure functional language, variables are immutable, too.
–
sepp2kOct 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. StanOct 16 '10 at 18:34

2

@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.
–
sepp2kOct 16 '10 at 18:45

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 Strings are also always immutable.