Alright, I'm trying to wrap my head around typeclasses, and so I'm trying to define a typeclass for geometric vector operations. I managed to get it working for component-wise +,-,*,/; but I'm struggling with the dot product.
class GeomVector a where
(>+) :: a -> a -> a
(>-) :: a -> a -> a
(>*) :: a -> a -> a
(>/) :: a -> a -> a
(>.) :: a -> a -> Double
data Vector a = Vec [a]
deriving Show
instance (Fractional a) => GeomVector (Vector a) where
(>+) (Vec u) (Vec v) = Vec $ zipWith (+) u v
(>-) (Vec u) (Vec v) = Vec $ zipWith (-) u v
(>*) (Vec u) (Vec v) = Vec $ zipWith (*) u v
(>/) (Vec u) (Vec v) = Vec $ zipWith (/) u v
(>.) (Vec u) (Vec v) = sum $ u >* v
Obviously my instance definition for (>.) won't work because the result is of type Fractional a, not Double.
But I don't know how to get this behavior from the declaration in the class.
What I'd like to do is:
class GeomVector [a] where
(>.) :: [a] -> [a] -> a
But this is invalid because [a] is a type and not a type variable.
I wish I could explain this a little better, but I honestly don't understand enough to do so. Hopefully the code will make it a little more obvious what I'm struggling with.
class GeomVector a s where ... (>.) :: a -> a -> s. - ErikRuandvwhich are lists, not instances of your class. - Dmitry Dzhusdata Scalar a)? - Dmitry DzhusScalar ashould be some existing type (for example, forinstance GeomVector [a]it should bea), not a brand new data type. - Lambdageek