-
Notifications
You must be signed in to change notification settings - Fork 1
/
1.31.tex
38 lines (36 loc) · 805 Bytes
/
1.31.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
\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\lstset{language=Lisp}
\begin{document}
\noindent
a.
\begin{lstlisting}
(define (product term a next b)
(if (> a b)
1
(* (term a) (product term (next a) next b))))
(define (factorial n)
(product (lambda (x) x)
1
(lambda (x) (+ x 1))
n))
(define (wallis n)
(define (next x) (+ x 2))
(if (even? n)
(* 8 n (/ (product square 4.0 next (- n 2))
(product square 3.0 next (- n 1))))
(* 2 (/ (product square 2.0 next (- n 1))
(* (product square 3.0 next (- n 2))
n)))))
\end{lstlisting}
\medskip \noindent
b.
\begin{lstlisting}
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* (term a) resut))))
(iter a 1))
\end{lstlisting}
\end{document}