-
Notifications
You must be signed in to change notification settings - Fork 0
/
s11hw
62 lines (52 loc) · 1.68 KB
/
s11hw
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
function add_streams(s1, s2) {
return is_null(s1)
? s2
: is_null(s2)
? s1
: pair(head(s1) + head(s2),
() => add_streams(stream_tail(s1),
stream_tail(s2)));
}
function scale_stream(c, stream) {
return stream_map(x => c * x, stream);
}
const add_series = add_streams;
const scale_series = scale_stream;
function negate_series(s) {
return scale_series(-1, s);
}
function subtract_series(s1, s2) {
return add_series(s1, negate_series(s2));
}
function coeffs_to_series(list_of_coeffs) {
const zeros = pair(0, () => zeros);
function iter(list) {
return is_null(list)
? zeros
: pair(head(list),
() => iter(tail(list)));
}
return iter(list_of_coeffs);
}
function fun_to_series(fun) {
return stream_map(fun, non_neg_integers);
}
const non_neg_integers = pair(0, () => stream_map(num => num + 1, non_neg_integers));
const ones = pair(1, () => ones);
const alt1 = pair(1, () => stream_map(num => -num, alt1));
const zeroes = add_series(alt1, stream_tail(alt1));
const zeroes2 = subtract_series(alt1, alt1);
eval_stream(alt1, 5);
eval_stream(zeroes, 5);
const s1 = x => fun_to_series(num => math_pow(x, num));
const s2 = x => {
return fun_to_series(num => (num + 1) * math_pow(x, num));
};
eval_stream(s2(2), 5);
eval_stream(fun_to_series(num => math_pow(2, num)), 5);
function mul_streams(a,b) {
return pair(head(a) * head(b),
() => mul_streams(stream_tail(a), stream_tail(b)));
}
const B = pair(1, () => mul_streams(B, stream_tail(non_neg_integers)));
eval_stream(B, 5);