
I have been trying Hugs98 This is the average of floats list calculation program: ================================ accum :: [Float] -> Float accum list = foldr (+) 0 list my_avg list = (accum list) / (length list) ================================ While loading it I got the compiler message: ERROR "U:\slav\FP\Avg.hs":11 - Type error in application *** Expression : accum list / length list *** Term : accum list *** Type : Float *** Does not match : Int Why "accum list" should match to "Int"? When I try to replace (length list) with number - it works. ================================ my_avg list = (accum list) / 5 --works fine ================================ xx = 5 my_avg list = (accum list) / xx --doesn't work -- same message as above ================================ Possibly, I don't know something important, or Hugs is not quite mature? Thank you. Slav.

ERROR "U:\slav\FP\Avg.hs":11 - Type error in application *** Expression : accum list / length list *** Term : accum list *** Type : Float *** Does not match : Int
Why "accum list" should match to "Int"? When I try to replace (length list) with number - it works.
length :: [a] -> Int -- this is your problem use genericLength :: Num k => [a] -> k instead
================================ my_avg list = (accum list) / 5 --works fine ================================
This works because '5' is translated to 'fromInteger 5', which can be a Float as desired.
xx = 5 my_avg list = (accum list) / xx --doesn't work -- same message as above
This doesn't work because defaulting occurs and xx is given type Integer. -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume

Yaroslav Korchevsky wrote:
my_avg list = (accum list) / (length list) ================================
ERROR "U:\slav\FP\Avg.hs":11 - Type error in application *** Expression : accum list / length list *** Term : accum list *** Type : Float *** Does not match : Int
Why "accum list" should match to "Int"?
(/) expects two equally typed arguments and (length list) yields the type Int.
When I try to replace (length list) with number - it works.
================================ my_avg list = (accum list) / 5 --works fine ================================ xx = 5 my_avg list = (accum list) / xx --doesn't work -- same message as above
Try to check the type of 5 and xx using ":t" at the hugs prompt.
Possibly, I don't know something important, or Hugs is not quite mature?
Haskell does at least allow literal numbers to fit several types. HTH Christian P.S. fromIntegral can be used for conversions

On 09-Dec-2003, Yaroslav Korchevsky
================================ my_avg list = (accum list) / 5 --works fine ================================ xx = 5 my_avg list = (accum list) / xx --doesn't work -- same message as above ================================
The infamous monomorphism restriction (Haskell Report section 4.5.5)
strikes again.
--
Fergus Henderson
participants (4)
-
Christian Maeder
-
Fergus Henderson
-
Hal Daume III
-
Yaroslav Korchevsky