[Haskell-beginners] Weighted average

Michael Snoyman michael at snoyman.com
Mon Nov 3 18:26:12 EST 2008


On Mon, Nov 3, 2008 at 2:55 PM, Daniel Fischer <daniel.is.fischer at web.de>wrote:

>
>
> > class Multiplicable a b c where
> >     mult :: a -> b -> c
>
> Use functional dependencies {-# LANGUAGE FunctionalDependencies #-},
>
> class Multiplicable a b c | a b -> c where ...
>
> which states that the result type of multiplication is determined by the
> argument types
>
> or type families


That's exactly what I was looking for, thank you. Now that I got that
working, I've noticed that it can be tedious making sure the arguments to
sumProduct are in the correct order. Since multiplication is commutative, is
there any way of automatically having the Multiplicable instances generate a
"flip" mult?

> sumProduct :: (Addable c, Multiplicable a b c) => [a] -> [b] -> c
> > sumProduct x y = sum $ product x y
> >
> > weightedAverage x y = (sumProduct y x) `divide` (sum y)
> >
> > class Dividable a b c where
> >     divide :: c -> a -> b
>
> FunDep here, too, but which one?


I did class Dividable a b c | c a -> b where...

Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081103/5f554cea/attachment.htm


More information about the Beginners mailing list