[Haskell-beginners] Average of numeric list
Nadav Chernin
nadavchernin at gmail.com
Tue Apr 5 11:57:32 CEST 2011
How can i know when casting of types maked by compiler and when programmer
must to do it?
On Tue, Apr 5, 2011 at 12:14 PM, Daniel Fischer <
daniel.is.fischer at googlemail.com> wrote:
> On Tuesday 05 April 2011 10:38:37, Nadav Chernin wrote:
> > Why only "length as" we must to cast? Why "sum as", that have type
> > Integer can be used in (/).
> >
> > :t (/)
> >
> > (/) :: (Fractional a) => a -> a -> a
>
> No, sum as has the type of as's elements,
>
> sum :: Num a => [a] -> a
>
> So the use of (/) refines the constraint from (Num a) to (Fractional a).
> if you want it to work on Integers too,
> you'd get
>
> mean :: (Real a, Fractional b) => [a] -> b
> mean xs = realToFrac (sum xs) / (fromIntegral $ length xs)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110405/fae08d4d/attachment.htm>
More information about the Beginners
mailing list