diff --git a/chapters/differential_equations/euler/code/haskell/euler.hs b/chapters/differential_equations/euler/code/haskell/euler.hs new file mode 100644 index 000000000..6ca9bf479 --- /dev/null +++ b/chapters/differential_equations/euler/code/haskell/euler.hs @@ -0,0 +1,20 @@ +solveEuler :: Num a => (a -> a) -> a -> a -> [a] +solveEuler f ts = iterate (\x -> x + f x * ts) + +checkResult :: (Ord a, Num a, Num t, Enum t) => a -> (t -> a) -> [a] -> Bool +checkResult thresh check = + and . zipWith (\i k -> abs (check i - k) < thresh) [0..] + +kinematics :: Double -> Double +kinematics x = -3 * x + +main :: IO () +main = + let timestep = 0.01 + n = 100 + threshold = 0.01 + checkResult' = checkResult threshold $ exp . (\x -> -3 * x * timestep) + in putStrLn $ + if checkResult' (take n $ solveEuler kinematics timestep 1) + then "All values within threshold" + else "Value(s) not in threshold" diff --git a/chapters/differential_equations/euler/euler.md b/chapters/differential_equations/euler/euler.md index bc766cab7..8cf7732b7 100644 --- a/chapters/differential_equations/euler/euler.md +++ b/chapters/differential_equations/euler/euler.md @@ -121,6 +121,9 @@ Full code for the visualization follows: {% sample lang="py" %} ### Python [import, lang:"python"](code/python/euler.py) +{% sample lang="hs" %} +### Haskell +[import, lang:"haskell"](code/haskell/euler.hs) {% endmethod %}