-
Notifications
You must be signed in to change notification settings - Fork 0
/
13th_Sept_list_and_trees_3_scale
143 lines (98 loc) · 2.71 KB
/
13th_Sept_list_and_trees_3_scale
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
// scaling a function by a factor of k
function scale_list2(xs, k) {
return is_null(xs)
? null
: pair(k * head(xs),
scale_list2(tail(xs), k));
}
scale_list2(list(1, 2, 3), 10);
// new type annotations: Pair<?,?>, List<?>, null
/*
function scale_list(xs : list<number>, k : number){
return is_null(xs)
? null
: pair(k * head(xs),
scale_list(tail(xs), k));
}
scale_list(list("A", "B", "C"), 10);
scale_list(list("A", "B", "C"), "D");
scale_list(list(1, 2, 3), "D");
*/
// squaring a lift
function square_list(xs) {
const square = x => x * x;
return is_null(xs)
? null
: pair(square(head(xs)),
square_list(tail(xs)));
}
square_list(list(1, 2, 3, 4));
// abstraction: map
// applying a function element wise to the list
function map(f, xs) {
return is_null(xs)
? null
: pair(f(head(xs)), map(f, tail(xs)));
}
// using map
function scale_list(xs, k) {
return map(x => k * x, xs);
}
function square_list(xs) {
return map(x => x * x, xs);
}
// making a copy of the list
function copy(xs) {
return map(x => x, xs);
}
const original = list(1, 3, 5);
const my_copy = copy(original);
// taking even numbers from the list
function even_numbers2(xs){
return is_null(xs)
? is_null
: head(xs) % 2 === 0
? pair(head(xs), even_numbers2(tail(xs)))
: even_numbers2(tail(xs));
}
// if we take head(xs) % 2 === 0 as the function
// abstration: filter
function filter(pred, xs){
return is_null(xs)
? null
: pred(head(xs)) // if predicate is true
? pair(head(xs), filter(pred, tail(xs)))
: filter(pred, tail(xs));
}
// based on the predicate, number of elements changes
// atmost n
// predicate is applied for each element
// it should be either true of false only
function even_numbers(xs){
const pred = x => x % 2 === 0;
return filter(pred, xxs);
}
// summing the elemnts of the list
function list_sum(xs){
return is_null(xs)
? 0
: head(xs) + list_sum(tail(xs));
}
// operations are going left to right
// but calculations are happing right to left
// due to deferred operations and applicative order reduction
// example if multiplication of all elements then * instead of +
// base case from 0 to 1
// abstraction: accumulate
function accumulate(op, initial, xs){ // op : operation, a function
return is_null(xs)
? initial
: op(head(xs), accumulate(op, initial, tail(xs)));
}
// rewriting list_sum
function list_sum2{
return accumulate((x,y) => x + y, 0, xs);
}
// (x,y) => x + y is the binary function that is why takes two arguments
// three higher order functions :
// map, filter, accumulate