Skip to content

Commit 1c27bc0

Browse files
markdown improvements
1 parent 68b1780 commit 1c27bc0

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

lexicalscoping.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ getting cached data
4848

4949
Understanding the concepts in terms of the behaviour of the two functions takes more than a few minutes because you have to not only read the existing exemplar functions but take some time to play with the functions. Often seeing the code in action is more helpful than reading a chapter about lexical scoping.
5050

51-
Variable `m` is declared uniquely in both functions and are allocated separate addresses in memory. In function makeVector() you’ll notice variable `m` is declared immediately and assigned the value NULL using the standard assignment operator (`<-`). However, the `set` functions defined within the containing makeVector() function require the special assignment operator (`<<–`) to update the value of variable `m`; it is important to remember variable m was declared and initialised by makeVector().
51+
Variable `m` is declared uniquely in both functions and are allocated separate addresses in memory. In function makeVector() you’ll notice variable `m` is declared immediately and assigned the value NULL using the standard assignment operator (`<-`). However, the `set` functions defined within the containing `makeVector()` function require the special assignment operator (`<<–`) to update the value of variable `m`; it is important to remember variable `m` was declared and initialised by `makeVector()`.
5252

5353
Had functions `set()` and `setmean()` not used the special assignment operator, these functions would have allocated memory to store the value and labelled the address as `m`. The variables named `m` would effectively be isolated and distinct variables.
5454

@@ -60,13 +60,13 @@ superassignment operator: `<<-`
6060

6161
```R
6262
crazy <- function() {
63-
x <<- 3.14 # variable x in the containing environment (global in this case) is updated to be 3.14
64-
print(x) # since no local variable ‘x’ exists within function ‘crazy’ R searches the containing environments
65-
{ print(x); # this is to demonstrate the function, not a code block, is the smallest environment in R
66-
x <- 42; print(x) # local variable ‘x’ is declared (created) and assigned the value 42; overrides the variable ‘x’ in
67-
} # the containing environment
68-
print(x) # since local variable ‘x’ now exists within the function there is no need to search the containing
69-
} # environment (global in this case)
63+
x <<- 3.14 # variable x in the containing environment (global in this case) is updated to be 3.14
64+
print(x) # since no local variable ‘x’ exists within function ‘crazy’ R searches the containing environments
65+
{ print(x); # this is to demonstrate the function, not a code block, is the smallest environment in R
66+
x <- 42; print(x) # local variable ‘x’ is declared (created) and assigned the value 42; overrides the variable ‘x’ in
67+
} # the containing environment
68+
print(x) # since local variable ‘x’ now exists within the function there is no need to search the containing
69+
} # environment (global in this case)
7070
> x <- 0
7171
> crazy()
7272
3.14
@@ -108,25 +108,25 @@ In the simplest example consider how variable ‘x’ changes when the crazy() f
108108

109109
```R
110110
# Declare and define a function named crazy()
111-
crazy <- function() { # create a new environment with a local variable ‘x’ and access to another variable ‘x’
112-
# declared somewhere outside this function
113-
x <- 3.14 # assign the numeric value 3.14 to local variable ‘x’
114-
print(x) # output the current value of local variable ‘x’ (1)
115-
{ print(x); # output the current value of local variable ‘x’ (2)
116-
x <<- 42; # assign the numeric value 42 to variable ‘x’ declared outside this function (3)
117-
print(x) # output the current value of local variable ‘x’ (4)
111+
crazy <- function() { # create a new environment with a local variable ‘x’ and access to another variable ‘x’
112+
# declared somewhere outside this function
113+
x <- 3.14 # assign the numeric value 3.14 to local variable ‘x’
114+
print(x) # output the current value of local variable ‘x’ (1)
115+
{ print(x); # output the current value of local variable ‘x’ (2)
116+
x <<- 42; # assign the numeric value 42 to variable ‘x’ declared outside this function (3)
117+
print(x) # output the current value of local variable ‘x’ (4)
118118
}
119-
print(x) # output the current value of local variable ‘x’ (5)
119+
print(x) # output the current value of local variable ‘x’ (5)
120120
}
121-
> x <- 0 # Declare and define a local variable named ‘x’
122-
> x # output the current value of local variable ‘x’
121+
> x <- 0 # Declare and define a local variable named ‘x’
122+
> x # output the current value of local variable ‘x’
123123
0
124-
> crazy() # Call function crazy()
125-
3.14 # (1) inner variable ‘x’
126-
3.14 # (2) inner variable ‘x’
127-
3.14 # (4) inner variable ‘x’
128-
3.14 # (5) inner variable ‘x’
129-
> x # (3) containing environment variable ‘x’
124+
> crazy() # Call function crazy()
125+
3.14 # (1) inner variable ‘x’
126+
3.14 # (2) inner variable ‘x’
127+
3.14 # (4) inner variable ‘x’
128+
3.14 # (5) inner variable ‘x’
129+
> x # (3) containing environment variable ‘x’
130130
42
131131
```
132132

@@ -139,8 +139,8 @@ x <- 3.14
139139
is treated as though it is as shown below.
140140

141141
```R
142-
x <- 3.24 # assigns the value 3.14 to local variable ‘x’ not the variable ‘x’ in the containing environment
143-
x <<- 42 # assigns the value 42 to variable ‘x’ in the containing environment
142+
x <- 3.24 # assigns the value 3.14 to local variable ‘x’ not the variable ‘x’ in the containing environment
143+
x <<- 42 # assigns the value 42 to variable ‘x’ in the containing environment
144144
```
145145

146146
Perhaps the crude graphic can illuminate the effects of lexical scoping on variables a better than mere words.
@@ -174,11 +174,11 @@ Flow of execution is (1a) -> [(2) & (3a)] -> [(3b) & (1b)] -> (1a). I used the l
174174
[,1] [,2]
175175
[1,] -2 1.5
176176
[2,] 1 -0.5
177-
> amatrix$getinverse() # Returns matrix inverse
177+
> amatrix$getinverse() # Returns matrix inverse
178178
[,1] [,2]
179179
[1,] -2 1.5
180180
[2,] 1 -0.5
181-
> cacheSolve(amatrix) # Returns cached matrix inverse using previously computed matrix inverse
181+
> cacheSolve(amatrix) # Returns cached matrix inverse using previously computed matrix inverse
182182
getting cached data
183183
[,1] [,2]
184184
[1,] -2 1.5

0 commit comments

Comments
 (0)