Help me please. I am a complete newcomer to HASKEL, I teach him less than a week.
There is a task:
“Implement a function that finds the value of a definite integral of a given function f on a given interval [a, b] by the trapezium method. (Use a uniform grid; 1000 elementary segments are enough.)
integration :: (Double -> Double) -> Double -> Double -> Double integration fab = undefined GHCi> integration sin pi 0 -2.0 The result may differ from -2.0, but not more than 1e-4. "
My code is:
integration fab | a < b = helper fab 0 ( (b - a) / 1000) | otherwise = -1 * integration fba helper fab res step | (abs (b - a)) < step = res + ( ((fa + fb) / 2) * abs (a - b) ) | otherwise = helper f (a + step) b (res + ( ( (fa) + (f (a + step))) / 2 ) * step ) step (It's a little bit wrong, but for some reason, not everything fits here.)
Falls on the first test: out of memory. Kill though I do not understand what was done wrong. What to change? How to improve?