-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.29.tex
39 lines (37 loc) · 820 Bytes
/
1.29.tex
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
\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\lstset{language=Lisp}
\begin{document}
Here's the function's definition
\begin{lstlisting}
(define (simpson-integral f a b n)
(define (iter k h sum)
(cond ((= k n)
(/ (* h (+ sum (f b)))
3))
((odd? k)
(iter (+ k 1)
h
(+ sum
(* (f (+ a (* k h)))
4.0))))
(else
(iter (+ k 1)
h
(+ sum
(* (f (+ a (* k h)))
2.0))))))
(iter 1 (/ (- b a) n) (f a)))
\end{lstlisting}
The following evaluations give us
\begin{lstlisting}
(simpson-integral cube 0 1 100)
\end{lstlisting}
$\rightarrow$ 0.25
\begin{lstlisting}
(simpson-integral cube 0 1 1000)
\end{lstlisting}
$\rightarrow$ 0.25000000000000006
So the results are more accurate than those of the procedure
\lstinline!integral!.
\end{document}