[Haskell-beginners] Re: question about styles of recursion
bbxx789_05ss at yahoo.com
Thu Mar 26 16:53:47 EDT 2009
7stud <bbxx789_05ss <at> yahoo.com> writes:
> > myAvg' :: Int -> [Int] -> [ Double ] -> Double
> > myAvg' sum count  = sum / fromIntegral count
> > myAvg' sum count (x:xs) = myAvg' (x + sum) (n + 1) xs
> The length function was introduced
> before the sum function, which I see you are using in your
> function definition.
Hmm...I guess you aren't using the sum function--that's one
of your variable names. I wonder how Prelude knows sum
is a variable name and not the sum function? I changed the
name sum to s, and I get this error:
Prelude Data.List> :load ehask.hs
[1 of 1] Compiling Main ( ehask.hs, interpreted )
Couldn't match expected type `Double' against inferred type `Int'
In the expression: s / fromIntegral count
In the definition of `myAvg'':
myAvg' s count  = s / fromIntegral count
Failed, modules loaded: none.
More information about the Beginners