fork download
  1. import Data.Ratio
  2.  
  3. f1, f2 :: (Num a) => a -> a
  4. f1 x = x^3 + x - 5
  5. f2 x = x^4 + 2*x - 5
  6.  
  7. euler :: (Rational -> Rational) -> Rational -> Rational -> [Rational]
  8. euler f a b = xs
  9. where xs = a : b : map step (zip (tail xs) xs)
  10. step (x,x0) = x - (f x)*(x - x0) / (f x - f x0)
  11.  
  12. main = do
  13. putStrLn "1. f1(x) = x^3 + x - 5 [1,2]"
  14. mapM_ printResult . take 5 $ euler f1 1 2
  15. putStrLn "\n2. f2(x) = x^4 + 2*x - 5 [-2,-1]"
  16. mapM_ printResult . take 5 $ euler f2 (-2) (-1)
  17. putStrLn "\n3. f2(x) = x^4 + 2*x - 5 [1,2]"
  18. mapM_ printResult . take 5 $ euler f2 1 2
  19.  
  20.  
  21. printResult :: Rational -> IO ()
  22. printResult x = do
  23. putStr . show $ x
  24. putStr " ---> "
Success #stdin #stdout 0s 6332KB
stdin
Standard input is empty
stdout
1. f1(x) = x^3 + x - 5     [1,2]
1 % 1  --->  1.0
2 % 1  --->  2.0
11 % 8  --->  1.375
914 % 617  --->  1.4813614262560777
37653118 % 24788479  --->  1.5189765374470938

2. f2(x) = x^4 + 2*x - 5     [-2,-1]
(-2) % 1  --->  -2.0
(-1) % 1  --->  -1.0
(-19) % 13  --->  -1.4615384615384615
(-12874) % 6283  --->  -2.0490211682317363
(-17948887681789277) % 11028014055859169  --->  -1.6275720715329571

3. f2(x) = x^4 + 2*x - 5     [1,2]
1 % 1  --->  1.0
2 % 1  --->  2.0
19 % 17  --->  1.1176470588235294
106759 % 90227  --->  1.1832267503075575
18498702470341302526 % 14607092337484868603  --->  1.266419219030316