Cool. It&#39;s more and more clear guys.<br>Thanks a lot. I&#39;ll check that &quot;expression problem&quot;.<br>&quot;Existential types&quot; sounds a bit scary :)<br><br><br><div class="gmail_quote">On Tue, Sep 15, 2009 at 3:26 PM, Sean Leather <span dir="ltr">&lt;<a href="mailto:leather@cs.uu.nl">leather@cs.uu.nl</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="gmail_quote"><div class="im"><div> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Perimeter doesn&#39;t make sense for Sphere or Cylinder. So we could define a type class for objects that have perimeter and make an instance of it only for Circle (data Circle = Circle Position Radius). Make sense. But these three functions above have desired behaviour. If user has a list of objects like [Sphere, Circle, Circle, Cylinder] he would like to calculate perimeters of each object using map perimerer list (in this case we also have to modify Geometry data type).<br>



So we could make instances of &quot;perimeter&quot; type class for all objects and return zero in case if perimeter doesn&#39;t make sense.<br>Same as previous version but with typeclasses and with additional constructors (constructors for each type of object + constructors in Geometry data). Looks a bit overcomplicated.<br>



Any reasons to use type classes in this case? Maybe there is something I&#39;m missing?<br></blockquote></div><div><br>If you&#39;re talking about a single datatype with multiple constructors, then the function &#39;perimeter :: Geometry -&gt; Maybe Double&#39; makes sense. If you&#39;re talking about multiple datatypes, then you probably want to go type class route.<br>


<br>data Sphere = Sphere ...<br>data Circle = Circle ...<br><br>class Perimeter a where perimeter :: a -&gt; Double<br>instance Perimeter Circle where perimeter (Circle ...) = ...<br>-- No instance for Sphere<br><br>class Volume a where volume :: a -&gt; Double<br>


instance Volume Sphere where volume (Sphere ...) = ...<br>-- No instance for Circle<br><br>You have to decide whether (1) a datatype Geometry makes sense or (2) a datatype per geometric entity is better. One advantage to #1 is that writing functions over the datatype is easy. One advantage to #2 is that you have fewer (partial) &#39;Maybe&#39; functions. This is also related to the &quot;expression problem,&quot; a Googleable term.<br>


<br>As for having a list of objects, you can do it with either approach. The second approach may require existential types.<br><br>Regards,<br>Sean<br></div></div>
</blockquote></div><br>