-
Notifications
You must be signed in to change notification settings - Fork 434
/
week2.R
61 lines (59 loc) · 1.09 KB
/
week2.R
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
# Quiz 2
# Problem 1.
cube <- function(x, n){
x^3
}
cube(3) # 27
# Problem 2.
x <- 1:10
if(x > 5) {
x <- 0
} # warning
# Problem 3.
f <- function(x) {
g <- function(y) {
y + z
}
z <- 4
x + g(x)
}
z <- 10
f(3) # 10
# Problem 4.
x <- 5
y <- if(x < 3) {
NA
} else {
10
}
y # 10
# Problem 5.
h <- function(x, y = NULL, d = 3L) {
z <- cbind(x, d)
if(!is.null(y))
z <- z + y
else
z <- z + f
g <- x + y / z
if(d == 3L)
return(g)
g <- g + 10
g
}
# free variable: f
f <- function(x, y) {
x^2 + y / z
} # free variable: z
# Problem 6.
# environment: a collection of symbol/value pair.
# Problem 7.
# type of scoping rule for resolving free variables: Lexical scoping
# Problem 8.
# free variables in R functions are resolved by:
# The values of free variables are searched for in the environment
# in which the function was defined.
# Problem 9.
# one of the consequences of the scoping rules used in R:
# All objects must be stored in memory.
# Problem 10.
# parent theme: it is the environment in which a function was called.