module Calculus where

myrange :: Double -> Double -> Double -> [Double]
myrange begin end gap = takeWhile (< end) (iterate (+ gap) begin)

integration :: (Double -> Double) -> Double -> Double -> Double
integration func begin end =
    let dh = 0.00001;
	    r = myrange begin end dh
	    in sum (map (* dh) (map func r))

derivatives :: (Double -> Double) -> Double -> Double
derivatives func x =
    let dh = 0.00001;
	    y = func x;
		y1 = func (x + dh)
	    in (y1 - y) / dh

newton :: (Double -> Double) -> Double -> Double -> Double
newton func init eps =
    let iter = \x -> x - (func x) / (derivatives func x)
	    in until (\x -> abs(func x) < eps) iter init

f :: Double -> Double
f x = x * x * x - 2

main = print (newton f 10 0.00001)

