Skip to content

Commit 76e34d1

Browse files
markdown improvements
1 parent 1c27bc0 commit 76e34d1

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

lexicalscoping.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,32 @@ superassignment operator: `<<-`
6161
```R
6262
crazy <- function() {
6363
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
64+
print(x) # since no local variable `x` exists within function ‘crazy’ R searches the containing environments
6565
{ 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
66+
x <- 42; print(x) # local variable `x` is declared (created) and assigned the value 42; overrides the variable `x` in
6767
} # the containing environment
68-
print(x) # since local variable ‘x’ now exists within the function there is no need to search the containing
68+
print(x) # since local variable `x` now exists within the function there is no need to search the containing
6969
} # environment (global in this case)
7070
> x <- 0
7171
> crazy()
7272
3.14
7373
3.14
7474
42
7575
42
76-
> x # variable ‘x’ outside of the function its updated value after the first statement within function ‘crazy()’
76+
> x # variable `x` outside of the function its updated value after the first statement within function ‘crazy()’
7777
[1] 3.14
7878
```
7979

80-
The first two print() statements use the variable ‘x’ in the containing environment, as no local variable ‘x’ exists at the moment, which has been updated from x <- 0 to x <- 3.14 via x <<- 3.14 inside function ‘crazy()’.
80+
The first two print() statements use the variable `x` in the containing environment, as no local variable `x` exists at the moment, which has been updated from x <- 0 to x <- 3.14 via x <<- 3.14 inside function ‘crazy()’.
8181

82-
The third print() statement uses the variable ‘x’ just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements.
82+
The third print() statement uses the variable `x` just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements.
8383

84-
The fourth print() statement uses the variable ‘x’ which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable ‘x’.
84+
The fourth print() statement uses the variable `x` which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable `x`.
8585

86-
I added a call to variable ‘x’ after the function ‘crazy()’ returns to show it keeps the new value assigned to it by the super-assignment operator inside function ‘crazy()’.
86+
I added a call to variable `x` after the function ‘crazy()’ returns to show it keeps the new value assigned to it by the super-assignment operator inside function ‘crazy()’.
8787

8888
The super-assignment operator does not update a variable of the same name inside an inner function but the innermost environment inherits any changes unless a local variable of the same name exists within the inner function as demonstrated by x <- 42; print(x) and print(x).
89-
Furthermore, if a variable named ‘x’ had existed inside function ‘crazy()’ and preceded the call to the super-assignment operator, the results would be as shown in the next example.
89+
Furthermore, if a variable named `x` had existed inside function ‘crazy()’ and preceded the call to the super-assignment operator, the results would be as shown in the next example.
9090

9191
```R
9292
crazy <- function() {
@@ -104,29 +104,29 @@ crazy <- function() {
104104
```
105105

106106
To reiterate the concept the next section explains in more detail the role and behaviour of the “superassignment” operator which allows the programmer to modify a variable declared outside of the current function in which the reference to the variable is made.
107-
In the simplest example consider how variable ‘x’ changes when the crazy() function is called.
107+
In the simplest example consider how variable `x` changes when the crazy() function is called.
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’
111+
crazy <- function() { # create a new environment with a local variable `x` and access to another variable `x`
112112
# 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)
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
124124
> 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’
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.

0 commit comments

Comments
 (0)