Sohail Butt
Sohail Butt

Reputation: 1

Haskell Help -- Calculating Mean of A List

I am struggling with this. And this is what I have attempted

mean :: [Float] -> Float
mean list = (sum list) / (fromInt (length list))

I am getting an error for fromInt. I have no idea why this is happening your help is much appreciated.

The error I get is : Not in scope: `fromInt'

And I can't use the recursive method.

If you have any tips and websites please do let me know

Thank You Very Much

Upvotes: 0

Views: 1139

Answers (3)

pardeep131085
pardeep131085

Reputation: 5518

Here is the example to calculate means

Let's define a function that accept list of number and return double

mean :: [Double] -> Double
mean (x) = (x1)
    where x1 = sum x / fromIntegral (length x)

main = do 
   print( mean [1..10])

Upvotes: 0

dfeuer
dfeuer

Reputation: 48591

The error saying something is "not in scope" simply means that you've used a name that doesn't mean anything in a particular place. In this case, it is because you wrote fromInt, and there is no such function available. In some cases, you may have to import a certain module in order to bring something into scope. For example, to use the inits function, you would have to import Data.List, and to use the Seq type you would have to import Data.Sequence.

A bit more subtly, you can't use a local variable outside its scope:

y = let x = 3 in x*5
z = x + 4   -- Error: x is not in scope

Although there is an x somewhere in the module, it's only in scope within the let expression.

Upvotes: 1

mariop
mariop

Reputation: 3216

You are using fromInt that doesn't exist and that's the error. For a short solution to the problem go to the end of this.

Let me start by saying that the reason why you need that fromInt (or a replacer, considering that's wrong) is because the / function exists on Fractional types. You can see it on GHCI:

Prelude> :t (/)
(/) :: Fractional a => a -> a -> a

Prelude> :t length
length :: [a] -> Int

The output of length list is and Int and Int is not a Fractional. You can check it from GHCI with :i that gives you also the instances of a type class:

Prelude> :i Fractional
class Num a => Fractional a where
  (/) :: a -> a -> a
  recip :: a -> a
  fromRational :: Rational -> a
      -- Defined in `GHC.Real'
instance Fractional Float -- Defined in `GHC.Float'
instance Fractional Double -- Defined in `GHC.Float'

So no, Int is not an instance of Fractional, only Float and Double are Fractional, and thus you cannot use /. You need to convert the output of length list to Fractional and my guess is that you were trying to use fromIntegral, that converts from an Integral to a Num, and you accidentally wrote fromInt.

I think this because fromIntegral is a type conversion function that "does what you need":

Prelude> :t fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b

the output of length list, is an Int that is Integral:

Prelude> :i Integral
class (Real a, Enum a) => Integral a where
  -- [...]
instance Integral Integer -- Defined in `GHC.Real'
instance Integral Int -- Defined in `GHC.Real'

and both Float and Double, that are Fractional and thus support /, are Num:

Prelude> :i Num
class Num a where
  -- [...]
instance Num Float -- Defined in `GHC.Float'
instance Num Double -- Defined in `GHC.Float

so by using it you can convert an Int to a Fractional type and make your function compile:

mean :: [Float] -> Float
mean list = sum list / fromIntegral (length list)

For a reference on how numeric types conversions work in Haskell, have a look at Converting Numbers of Haskell.org.

Upvotes: 3

Related Questions