/usr/share/doc/r5rs-doc/r5rs/Example.html is in r5rs-doc 20010328-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | <html lang="en">
<head>
<title>Example - Revised(5) Scheme</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Revised(5) Scheme">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="prev" href="Additional-material.html#Additional-material" title="Additional material">
<link rel="next" href="Bibliography.html#Bibliography" title="Bibliography">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Example"></a>
<p>
Next: <a rel="next" accesskey="n" href="Bibliography.html#Bibliography">Bibliography</a>,
Previous: <a rel="previous" accesskey="p" href="Additional-material.html#Additional-material">Additional material</a>,
Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a>
<hr>
</div>
<h2 class="unnumbered">Example</h2>
<!-- *- Mode: Lisp; Package: SCHEME; Syntax: Common-lisp -*- -->
<p><a name="index-g_t_0040w_007bexample_007d-496"></a>
‘<samp><span class="samp">Integrate-system</span></samp>’ integrates the system
<div align="center">y_k^^ = f_k(y_1, y_2, <small class="dots">...</small>, y_n), k = 1, <small class="dots">...</small>, n</div>
<p>of differential equations with the method of Runge-Kutta.
<p>The parameter <tt>system-derivative</tt> is a function that takes a system
state (a vector of values for the state variables y_1, <small class="dots">...</small>, y_n)
and produces a system derivative (the values y_1^^, <small class="dots">...</small>,y_n^^). The parameter <tt>initial-state</tt> provides an initial
system state, and <tt>h</tt> is an initial guess for the length of the
integration step.
<p>The value returned by ‘<samp><span class="samp">integrate-system</span></samp>’ is an infinite stream of
system states.
<pre class="example">
(define integrate-system
(lambda (system-derivative initial-state h)
(let ((next (runge-kutta-4 system-derivative h)))
(letrec ((states
(cons initial-state
(delay (map-streams next
states)))))
states))))
</pre>
<p>‘<samp><span class="samp">Runge-Kutta-4</span></samp>’ takes a function, <tt>f</tt>, that produces a
system derivative from a system state. ‘<samp><span class="samp">Runge-Kutta-4</span></samp>’
produces a function that takes a system state and
produces a new system state.
<pre class="example">
(define runge-kutta-4
(lambda (f h)
(let ((*h (scale-vector h))
(*2 (scale-vector 2))
(*1/2 (scale-vector (/ 1 2)))
(*1/6 (scale-vector (/ 1 6))))
(lambda (y)
;; y is a system state
(let* ((k0 (*h (f y)))
(k1 (*h (f (add-vectors y (*1/2 k0)))))
(k2 (*h (f (add-vectors y (*1/2 k1)))))
(k3 (*h (f (add-vectors y k2)))))
(add-vectors y
(*1/6 (add-vectors k0
(*2 k1)
(*2 k2)
k3))))))))
<!-- |-| -->
(define elementwise
(lambda (f)
(lambda vectors
(generate-vector
(vector-length (car vectors))
(lambda (i)
(apply f
(map (lambda (v) (vector-ref v i))
vectors)))))))
<!-- |-| -->
(define generate-vector
(lambda (size proc)
(let ((ans (make-vector size)))
(letrec ((loop
(lambda (i)
(cond ((= i size) ans)
(else
(vector-set! ans i (proc i))
(loop (+ i 1)))))))
(loop 0)))))
(define add-vectors (elementwise +))
(define scale-vector
(lambda (s)
(elementwise (lambda (x) (* x s)))))
</pre>
<p>‘<samp><span class="samp">Map-streams</span></samp>’ is analogous to ‘<samp><span class="samp">map</span></samp>’: it applies its first
argument (a procedure) to all the elements of its second argument (a
stream).
<pre class="example">
(define map-streams
(lambda (f s)
(cons (f (head s))
(delay (map-streams f (tail s))))))
</pre>
<p>Infinite streams are implemented as pairs whose car holds the first
element of the stream and whose cdr holds a promise to deliver the rest
of the stream.
<pre class="example">
(define head car)
(define tail
(lambda (stream) (force (cdr stream))))
</pre>
<pre class="sp">
</pre>
The following illustrates the use of ‘<samp><span class="samp">integrate-system</span></samp>’ in
integrating the system
<div align="center">C dv_C / dt = -i_L - v_C / R</div>
<div align="center">L di_L / dt = v_C</div>
<p>which models a damped oscillator.
<pre class="example">
(define damped-oscillator
(lambda (R L C)
(lambda (state)
(let ((Vc (vector-ref state 0))
(Il (vector-ref state 1)))
(vector (- 0 (+ (/ Vc (* R C)) (/ Il C)))
(/ Vc L))))))
(define the-states
(integrate-system
(damped-oscillator 10000 1000 .001)
'#(1 0)
.01))
</pre>
<!-- (letrec ((loop (lambda (s) -->
<!-- (newline) -->
<!-- (write (head s)) -->
<!-- (loop (tail s))))) -->
<!-- (loop the-states)) -->
<!-- #(1 0) -->
<!-- #(0.99895054 9.994835e-6) -->
<!-- #(0.99780226 1.9978681e-5) -->
<!-- #(0.9965554 2.9950552e-5) -->
<!-- #(0.9952102 3.990946e-5) -->
<!-- #(0.99376684 4.985443e-5) -->
<!-- #(0.99222565 5.9784474e-5) -->
<!-- #(0.9905868 6.969862e-5) -->
<!-- #(0.9888506 7.9595884e-5) -->
<!-- #(0.9870173 8.94753e-5) -->
<!-- \newpage % Put bib on it's own page (it's just one) -->
<!-- \twocolumn[\vspace{-.18in}]% Last bib item was on a page by itself. -->
<!-- \renewcommand{\bibname}{References} -->
<!-- @include{bib} -->
<!-- My reference for proper reference format is: -->
<!-- Mary-Claire van Leunen. -->
<!-- {\em A Handbook for Scholars.} -->
<!-- Knopf, 1978. -->
<!-- I think the references list would look better in ``open'' format, -->
<!-- i.e. with the three blocks for each entry appearing on separate -->
<!-- lines. I used the compressed format for SIGPLAN in the interest of -->
<!-- space. In open format, when a block runs over one line, -->
<!-- continuation lines should be indented; this could probably be done -->
<!-- using some flavor of latex list environment. Maybe the right thing -->
<!-- to do in the long run would be to convert to Bibtex, which probably -->
<!-- does the right thing, since it was implemented by one of van -->
<!-- Leunen's colleagues at DEC SRC. -->
<!-- Jonathan -->
<!-- I tried to follow Jonathan's format, insofar as I understood it. -->
<!-- I tried to order entries lexicographically by authors (with singly -->
<!-- authored papers first), then by date. -->
<!-- In some cases I replaced a technical report or conference paper -->
<!-- by a subsequent journal article, but I think there are several -->
<!-- more such replacements that ought to be made. -->
<!-- Will, 1991. -->
<!-- This is just a personal remark on your question on the RRRS: -->
<!-- The language CUCH (Curry-Church) was implemented by 1964 and -->
<!-- is a practical version of the lambda-calculus (call-by-name). -->
<!-- One reference you may find in Formal Language Description Languages -->
<!-- for Computer Programming T.~B.~Steele, 1965 (or so). -->
<!-- Matthias Felleisen -->
<!-- Rather than try to keep the bibliography up-to-date, which is hopeless -->
<!-- given the time between updates, I replaced the bulk of the references -->
<!-- with a pointer to the Scheme Repository. Ozan Yigit's bibliography in -->
<!-- the repository is a superset of the R4RS one. -->
<!-- The bibliography now contains only items referenced within the report. -->
<!-- Richard, 1996. -->
</body></html>
|