-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11th_October_environment_blocks_functions
90 lines (49 loc) · 1.51 KB
/
11th_October_environment_blocks_functions
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
// new declaration in new block/scope means new frame in cse machine
// functions
// from chapter 1:
/*
function make_adder(x){
return y => x + y;
}
const add_4 = make_adder(4);
add_4(5);
*/
// but in cse machine: no substitution model
// so how does add_4 know that x is 4
// new frame with the parameters when we use y => x + y;
// every function application extends the environment in which the
// function was created
// the new frame contains bindings of parameters to arguments
// no new frame if function has no parameters
// function body block evaluated in this new environment
// for lambda expressions
// create a function object, represented as two circles
// one circle points to body (text)
// other circle to the environment in which the lambda expression was evaluated
// closures
// VERY IMPORTANT:
// a function remembers the environment in which it was created
// DONT FORGET^
const square = x => x * x;
square(5);
// evaluating a function application
// evaluate subexpressions in current environmet
// then (somethinggg)
// create a new frame that points to the environment of the function
// new frame - bind the parameters to the arguments
// parameters are variables so assignment allowed
// evaluate the body block in the new environment
/*
.
.
.
.
.
*/
// state and identity
// const a = pair(2,3);
// const b = a;
// a === b; returns true cause points to same pair
// const a = pair(2,3);
// const b = pair(2,3);
// a === b; returns false cause now points to diff pair