module PRO where
average1 :: IO Float
avarage1 =
do
putStrLn "Enter Marks in Form of a List"
marks <- getLine
let m = (read marks)::[Int]
x<-(sum' (m))
avg <- x/length (m)
if(x/=[])
then
putStrLn ("empty List")
else
putStrLn ("Your Avarage is " ++ show(avg))
sum' :: (Num a) => [a] -> a
sum' xs = foldl (\acc x -> acc + x) 0 xs
My program doesn’t seems to work! My question is why can’t I assign avg the returning sum of the sum' function?
You can’t use the
<-notation for assigning the return values ofsum' mandx/length m.<-can only be used when the right-hand side is a monadic expression (in this case, anIOvalue), which neither one is, and so you should uselet x = sum' mandlet avg = x / fromInteger (length m)instead (fromIntegeris needed to convert theIntreturned bylength mto a Fractional value so it can be passed to/). (Also, you need to changex /= []tom /= [].)