Reputation: 1
I'm trying to use one function to find the mean of a list in Haskell. This is what I put in:
let listmean x = (foldl (+) 0x)/(length x)
And I get this:
<`interactive>:43:18: error: • Could not deduce (Fractional Int) arising from a use of ‘/’ from the context: Foldable t bound by the inferred type of listmean :: Foldable t => t Int -> Int at :43:5-42 • In the expression: (foldl (+) 0 x) / (length x) In an equation for ‘listmean’: listmean x = (foldl (+) 0 x) / (length x)
I've tried to use the built-in div function but no luck there either. I run Haskell on https://replit.com, could it be the website?
Upvotes: 0
Views: 181
Reputation: 13939
/
takes two Fractional
s (try :info (/)
in ghci). So you need to convert the Int
into, say, Double
:
main = print $ mean [0,1,1,2,3,5,8] -- 2.857142857142857
mean :: [Int] -> Double
mean xs = fromIntegral (sum xs) / fromIntegral (length xs)
Upvotes: 0