datatype RealList CONS of real unit RealList NIL fun take xq nil take

 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
datatype RealList =
CONS of real * (unit -> RealList) | NIL
fun take (xq, 0) = nil
| take (NIL, n) = nil
| take (CONS (x,xf), n) = x :: take (xf(),n-1)
fun pow(m,n):real =
if n = 0 then 1.0
else m * pow(m,n-1);
fun fact(n):int =
if n = 0 then 1
else n * fact(n-1);
fun sum (nil) =
0.0 | sum (head::tail) = head+sum(tail);
fun TailorRange (x,n) =
CONS(pow(x, n)/real(fact(n)), fn() => TailorRange(x,n+1))
fun n_exp(n, eps):real =
if List.nth(take(TailorRange(1.0, 0),n), n-1) < eps
then sum(take(TailorRange(1.0, 0),n+1))
else n_exp(n+1, eps);
fun exp(eps):real = n_exp(1, eps);
val delta = 2.71828182846-exp(0.001);