[erlang-questions] Using different number types

Jay Anderson horndude77@REDACTED
Mon May 28 01:10:37 CEST 2007

>From what I've learned so far erlang has two numeric types: integers and floats.
I'd like to define some other types (rational, polynomial, vector, matrix) which
all support similar operations (add, subtract, multiply, divide, and various
comparison operators). I've already defined a rational type (see
http://en.literateprograms.org/Bernoulli_numbers_%28Erlang%29), but I'd like to
use all these types only through an interface. (I'm coming from the java and
ruby worlds where this is handled by defining objects which all implement the
same interface so if I'm way off base here let me know.) For example in making a
simple matrix module I'd like to be able to put rational numbers, polynomials,
floats, or whatever in a matrix and it will just work as long as that interface
it supported.

An idea I've thought of is to define a 'numeric operations' module which
represents these operations and knows how to delegate to the appropriate module.
There's still the problem of numeric coercion which I was thinking of solving
this in a way similar to ruby's coerce method. The problem with this is that
whenever a new type is created this 'numeric operations' module has to be
updated also.

Another idea would be to make each number a process and do something like: Num !
{self(), add, OtherNum}. and then receive the result. This way I don't need to
know anything about the types that I'm using in order to add them. I haven't
though much about the detail of this however.

What is the best way to handle this sort of problem in erlang?

(I know this is "off erlang's beaten path" but I'm having fun messing around.)


More information about the erlang-questions mailing list