If you want to compute the average of a bunch of Integers, you'll have to divide at some point, so you'll have to convert them from Integers to the division-supporting type of your choice. A close look at the Num class ( :i Num in ghci) reveals one problem with the type of avg : Num doesn't have enough methods — basically enough to add, multiply, and subtract. There's no guarantee that the number I give to avg can be converted to a Double at all.
If you want to compute the average of a bunch of Integers, you'll have to divide at some point, so you'll have to convert them from Integers to the division-supporting type of your choice. A close look at the Num class (:i Num in ghci) reveals one problem with the type of avg: Num doesn't have enough methods — basically enough to add, multiply, and subtract. There's no guarantee that the number I give to avg can be converted to a Double at all.
If you enter an untyped function to compute an average, Haskell responds with the most generic type possible: Prelude List> :type \x -> sum x / genericLength x \x -> sum x / genericLength x :: (Fractional a) => a -> a So that's the correct type of avg. You might notice that avg 1,2,3 :: Integer gives a type error. You can get around that by passing the argument to toRational or fromIntegral first, which use the Real and Integral instances for Integer, respectively.
Regarding the expression sum 1,2,3 / len 1,2,3: It's true that a literal number like 1 has the type of Num a => a, which calls fromInteger on whatever type it turns out to be, but an expression like 1/2 has a more specific type of Fractional a => a, which you can see if you ask for the type of that expression instead of printing it out. Something that might be helpful is :set -Wall in ghci, which turns on lots of warnings whenever a default type is chosen for you, giving a clue that the most generic type might no longer be correct.
I've chosen this as the answer, (definitely gives the most effort) but I'd like a quick follow up with a resource of where I can find what the prelude has for types, and type-classes regarding number-like types. What types of integers am I precluding by requiring the inputs be apart of Fractional? I've never seen a type apart of this typeclass prior to doing `:t (/) and I'd like to know more about how it works.
And, things magically join the class for the division – Evan Carroll Nov 29 '09 at 23:44 2 All those declarations are in the Prelude. Online reference here: haskell. Org/ghc/docs/6.10.2/html/libraries/base/Prelude.
Html – Chuck Nov 29 '09 at 23:53 I once saw a directed acyclic graph showing the numeric type class hierarchy, but I can't find it now. Here's a gentle introduction: (haskell. Org/tutorial/numbers.
Html). You can tell ghci to give you more info by using :i (/), or :i on a type or class. – Josh Lee Nov 29 '09 at 23:56.
You're overly constraining the type of avg. Use the more general version, avg :: (Fractional a) => a -> a.
My question would be, why is the stuff returned by len is part of the member to the Fractional typeclass, :t sum 1,2,3 and :t len 1,2,3 both show the result is only part of the Num typeclass. I'm confused how a function that requires its arguments to be apart of the Fractional typeclass can even be applied here. – Evan Carroll Nov 29 '09 at 23:21 EvanCarroll: The Num typeclass does not provide fractional division, but the Fractional typeclass does.
Since avg uses / in its top-level calculation the type signature must reflect that. When you make this change the compiler knows to use floating point math in sum and len. – Michael Steele Nov 30 '09 at 18:24.
Hhm the really problem is if it is an integral type you want to cast to a fractional type, but if it is an fractional type you want to leave it alone. Try this fromRational ((sum xs) % (leng xs)).
Mauke in irc 17:14 EvanCarroll: % requires integers talking to him is too difficult. , and I'd rather not ask him what he means by it. :t (%) isn't showing anything useful either.
– Evan Carroll Nov 29 '09 at 23:17 1 @Evan: upshaw's code uses haskell. Org/ghc/docs/latest/html/libraries/base/Data-Ratio. Html -- It only works if the list elements are integers.
– Stephan202 Nov 29 '09 at 23:32.
I've encountered this problem. This best I've managed to do is have two functions for averaging: one for integrals and one for fractionals: avgInt :: (Integral i, Fractional f) => I -> f avgInt xs = fromIntegral (sum xs) / fromIntegral (length xs) avgFrac :: (Fractional f) => f -> f avgFrac xs = sum xs / fromIntegral (length xs).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.