From da1a02298e0fd91006ccf49ace7218efcfb7fedb Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 4 Jul 2018 11:13:17 -0500 Subject: [PATCH 1/6] Added Forward Euler in Haskell --- .../euler/code/haskell/euler.hs | 13 +++++++++++++ chapters/differential_equations/euler/euler.md | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 chapters/differential_equations/euler/code/haskell/euler.hs 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..c82e33776 --- /dev/null +++ b/chapters/differential_equations/euler/code/haskell/euler.hs @@ -0,0 +1,13 @@ +solve_euler timestep n = take n $ iterate f 1 + where f x = x - 3 * x * timestep + +check_result results threshold timestep = + and $ zipWith check' results [exp $ -3 * i * timestep | i <- [0..]] + where check' result solution = abs (result - solution) < threshold + +main = let timestep = 0.01; + n = 1; + threshold = 0.01; + in putStrLn $ if check_result (solve_euler timestep n) threshold timestep + 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 %}