[mercury-users] maintaining state in instances of typeclasses

Terrence Brannon princepawn at earthlink.net
Sun May 13 01:51:10 AEST 2001


Hi, I was hoping that someone would complete this program for
me. Perhaps it is my fault, but after reading the manual, I don't know
how to create separate instances of typeclasses, each with their own
state. I have begun a classic example of polymorphism, but cannot
finish it. If someone could close this down for me, I would appreciate
it. If no one is willing to help me, then I will be in a state of
despair, with no obvious recourse as there is no commercial support
channel for Mercury.

/*
 1. enter square's length via STDIN
 2. enter circle's radius via STDIN
 3. print area of both, along with the value of each component of the computation. meaning, for
 squares, print the length and the area. For circles, print the radius and the area. Also, 
 if a previous area was calculated, print it as well. then store the new area so that it can be
 printed as the previous area on the next iteration.

 4. repeat 1, 2, 3 one time, creating new instances of both circle and square
 5. print the differences in area between the first and second square
 6. print the differences in area between the first and second circle
*/
 
typeclass shape(SHAPE) where [

func area = float.

].

instance shape(square) where [

func area = Length * Length.

].

instance shape(circle) where [

func area = 3.141 * Radius * Radius.

].

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list