In de book Modern C++ design, Andrei Alexandrescu writes that Haskell
supports “multi-methods”

http://books.google.com/books?id=aJ1av7UFBPwC&pg=PA3&ots=YPiJ_nWi6Y&dq=moder
n+C%2B%2B&sig=FWO6SVfIrgtCWifj9yYHj3bnplQ#PPA263,M1

How is this actually done in Haskell? Maybe this is just a basic feature of
Haskell which I don't grasp yet because of my object-oriented background?

A good example is collision between pairs of objects of type (a,b). In
object oriented languages this cannot be handled in a nice way, because
neither a.Collide(b) or b.Collide(a) is the correct approach; one would like
to write (a,b).Collide()

A specific example might be better here. 

Assume the following class hierarchy:

Solid
|
+-- Asteroid
|
+-- Planet
    |
    + -- Earth
    |
    + -- Jupiter

Using multi-methods, I could write (in pseudo code)

collide (Asteroid, Planet) = "an asteroid hit a planet"
collide (Asteroid, Earth) = "the end of the dinos"
collide (Solid,Solid) = " solids collided"
collide (Planet, Asteroid) = collide (Asteroid, Planet)
collide (Earth, Asteroid)  = collide (Earth, Asteroid)

So basically, the "best" collide function is picked, depending on the type
of the arguments.

How should I write Haskell code for something like this in general, in the
sense that this hierarchy is typically huge and the matrix (of collide
functions for each pair of types) is very sparse.

Thanks,
Peter




No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.476 / Virus Database: 269.11.6/938 - Release Date: 05/08/2007
16:16
 

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to