On Mon, 09 Jun 2003, Mark A. Biggar wrote:
multi factorial (0) { 1 } multi factorial ($n) { $n * factorial($n - 1) }
That's a bad example, as it's really not MMD. It's a partially pre-memoized function instead.
It is MMD as long as the dispatching mechanism (of the language or the engine it runs on) decides on runtime which one to use. It means you can't tell which gets called just looking at
factorial($n)
MMD can be thought as a I<RUNTIME> decision scheme for dispatching based
on belonging to a certain set of values. It is done in many modern
functional languages, Prolog and others too. I think Dan has
said that before: it is by no means a privilege of object-oriented languages.
If you don't write the code telling who gets called at runtime, I think it is MMD. It constrasts to
sub factorial($n) { ($n==0) ? 1 : $n*factorial($n-1) }
which doesn't give you the chance to say something like
multi factorial(Real x) { factorial(floor x) }
without tweaking the already functioning code. (Sorry for the awful example, but I didn't remember the relation to the gamma function.)
The runtime aspect is what makes it different from overloading like Java does in compile time. For example,
class SpecialMap { void put( String key, Object val ) { ... } void put( Object key, Object val ) { ... } }
SpecialMap map; ... String s = "abc"; Object o = s; map.put(s, null);
ends with put(Object, Object) being called, because it is decided on compile time.
Parrot will support natively MMD based on the type, which is a sort of belonging to a set of values which can be efficiently implemented.
The issue of implementing the factorial (or any other function) defined by parts like the example that sparked this discussing with pre-memoized returns is an entire different question. No one said that once we computed factorial(24), factorial(25) would be a lot quicker because it could use the pre-memoized value in the recursive definition. Certain functional languages do it, but nothing here refers to this.
In my opinion, MMD main features are runtime dispatch decision and (to a dynamic language) seamless extension by adding new patterns.
Regards,
Adriano.
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/