Extending arithmetic

James Hague <>
Fri Feb 7 17:59:03 CET 2003


In graphics work vector math comes up a lot, and it occurred to me that much
of this can be done in Erlang without changing the syntax at all.  Rather
than simply allowing "number + number -> number," allow the following:

tuple + number -> tuple
ex: {1,2,3} + 4 -> {4,6,7}

tuple + tuple -> tuple
ex: {1,2,3} + {4,5,6} -> {5,7,9}

-tuple -> tuple
-{1,2,3} - {-1,-2,-3}

The first three examples would work for the operators +, -, *, /, and rem.
For the latter two, "number op tuple" is invalid, but it's okay for the
others.  In the "tuple op tuple" case, it's an error if the tuples are of
different lengths.

Pros:
1. Adding two tuples makes perfect sense to someone who sees it for the
first time.  It's the expected behavior.
2. Increase in expressiveness; plays off of the dynamic nature of the
language.
3. No syntax changes at all.
4. Improves the math performance of Erlang, one of it's weaker points.

Cons:
1. Yet Another Feature.
2. Adds some complexity to the emulator.
3. Could open the door for additional cases.  What about bit operators like
bor and band?  Why is there no built-in to sum a tuple?

As this only requires an emulator change, I'd give it a try if I could
compile the code :)

I don't expect this to be taken seriously, but I think it's an interesting
idea.



More information about the erlang-questions mailing list