I'm very new to Haskell and I'm trying to make a basic function which finds the mean of a list. I've almost got it solved, except for the last bit of division I have to do.
randInts = take 5 $ randoms (mkStdGen 11) :: [Int]
listSum :: (Num a) => [a] -> a
listSum [] = 0
listSum (x:xs) = x + listSum xs
listLength :: (Num a) => [a] -> a
listLength [] = 0
listLength (x:xs) = 1 + listLength xs
listMean :: (Num a) => [a] -> a
listMean [] = 0
listMean x = (listSum x) / (listLength x)
This is what I started with, where listSum and listLength basically have the same functionality as the sum and length operators on lists respectively and listMean takes those two and divides them. When I run this, however, I get an error saying "Could not deduce (Fractional a) arising from a use of ‘/’ from the context: Num a"
At first, I just thought I had used the wrong type and switched listMean to start with listMean :: (Fractional a) => [a] -> a. This allowed me to compile, but when I attempted to run it by giving it randInts, but instead I got the error message "No instance for (Fractional Int) arising from a use of ‘listMean’."
I am not sure what I am missing here. Could I have some advice please?