summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--chapter-3/3.1-combining-arithmetics.scm43
1 files changed, 43 insertions, 0 deletions
diff --git a/chapter-3/3.1-combining-arithmetics.scm b/chapter-3/3.1-combining-arithmetics.scm
new file mode 100644
index 0000000..08210b9
--- /dev/null
+++ b/chapter-3/3.1-combining-arithmetics.scm
@@ -0,0 +1,43 @@
+;;; Copyright © 2021 Gerald Sussman and Chris Hanson
+;;; Copyright © 2022 David Thompson <dthompson2@worcester.edu>
+;;;
+;;; This program is free software: you can redistribute it and/or
+;;; modify it under the terms of the GNU General Public License as
+;;; published by the Free Software Foundation, either version 3 of the
+;;; License, or (at your option) any later version.
+;;;
+;;; This program is distributed in the hope that it will be useful,
+;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with this program. If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+(define (stormer-2 F h)
+ (lambda (history)
+ (+ (* 2 (x 0 history))
+ (* -1 (x 1 history))
+ (* (/ (expt h 2) 12)
+ (+ (* 13 (F (t 0 history) (x 0 history)))
+ (* -2 (F (t 1 history) (x 1 history)))
+ (F (t 2 history) (x 2 history)))))))
+
+(define (stepper h integrator)
+ (lambda (history)
+ (extend-history (+ (t 0 history) h)
+ (integrator history)
+ history)))
+
+(define n:> >)
+(define n:- -)
+
+(define (evolver F h make-integrator)
+ (let* ((integrator (make-integrator F h))
+ (step (stepper h integrator)))
+ (define (evolve history n-steps)
+ (if (n:> n-steps 0)
+ (evolve (step history) (n:- n-steps 1))
+ history))
+ evolve))