[Haskell-beginners] typeclass confusion

Brandon S Allbery KF8NH allbery at ece.cmu.edu
Tue Aug 24 01:56:52 EDT 2010

Hash: SHA1

On 8/24/10 01:38 , Greg Best wrote:
> The part I figured was impractical was the "Angular" class, but this is the second feedback that suggested making both Degrees and Radians type constructors rather than alternate value constructors.  Is that the right approach?  I know the trig functions are already available and they all just traffic in floats, but if this weren't the case, I'd imagine a structure along the lines of

The point of typeclasses is to allow you to abstract operations over
multiple types.  If you have only one type...

> data Angle a = Radians a
>             | Degrees a
>             deriving (Eq, Show)

then a typeclass is just unnecessary complexity.  It's when you have
multiple types which share a group of operations that typeclasses become
useful.  So, for example, with the above type (sin) can be implemented as a
single function which pattern matches on the constructor; but if (Radians)
and (Degrees) need to be separate types instead of value constructors for a
single type, then you *must* use a typeclass to implement a single (sin)
instead of (sinRadians) or (sinDegrees).

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]      allbery at kf8nh.com
system administrator  [openafs,heimdal,too many hats]  allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university      KF8NH
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/


More information about the Beginners mailing list