Starter Files
Download this playground file with the problems inside.
Reading
This lesson’s content corresponds to the “Control Flow” & “Closures” sections of The Swift Programming Language book.
Submission
After you complete all the problems inside the playground file, upload at the bottom of this page a screenshot of the playground with all the tests passed. An example is shown below:
What Would Swift Display? (WWSD) problems:
—–Section One—–
Consider the statement below
let a = { (x: Int) in // A closure with one paramter x
return x
}
Q1: What does a(5) evaluate to?
Consider the statement below
let b = { () in /* Using a closure as an operator in a call expression */
return 3
}()
Q2: What does b evaluate to?
Consider the statements below
let c = { (x: Int) in // Closures can return other closures!
return { (x: Int) in
return x
}
}
let d = c(88)
Q3: What does d(42) evaluate to?
Consider the statements below
func square(x: Int) > Int {
return x*x
}
let e = { (math: (Int) > Int) in // They can have functions arguments as well
return math(4)
}
Q4: What does e(square) evaluate to?
Consider the functions below
func foo(x: Int) > ((Int) > Int) {
func lol(x: Int) > ((Int) > Int){
return { (x: Int) in
return x + 1
}
}
return lol(x: x+1)
}
Q5: What does foo(x: 3)(5) evaluate to?
Consider the following assignment statement:
let deadpool = foo(x: 1)
Q6: What does deadpool(2) + deadpool(0) evaluate to?
Q7: How many functions are there?
Q8: How many functions have intrinsic names?
Consider the function below.
func marvel(cap: [Int]) > Int {
var thor = cap, marvel = 0, iron = cap.count
while iron > 0 {
var hulk = thor[..<iron]
print(hulk)
marvel += hulk[hulk.count1]
iron = 1
}
return marvel
}
Q9: What does marvel(cap: [1, 2, 3]) evaluate to?
Q10: What does marvel(cap: [1, 0]) evaluate to?
Consider the function below:
func poke(mon: [Int]) > Int {
var pikachu = 1, snorlax = 0
for poke in mon {
if poke == pikachu{
continue
} else if poke == pikachu + 1{
break
}
snorlax += poke
}
return snorlax
}
Q11: What does poke(mon: [1, 2, 2, 2]) evaluate to?
Q12: What does poke(mon: [5, 3, 2, 3]) evaluate to?
Coding Practice
Q1: Make Adder With Closure
Implement the makeAdder function, which takes in a number n and returns a function that takes in an another number k and returns n + k. Your solution must consist of a single return statement.
let addThree = makeAdder(n: 3)
addThree(1) + addThree(2) evaluates to 9
makeAdder(n: 1)(2) evaluates to 3
/**
Returns a function that adds n to its input k.
 Parameter n: the number to add to inputted numbers.
 Returns: a function that adds n to some inputted number k.
*/
func makeAdder(n: Int) > ((Int) > Int) {
return ___ // You can only have this one return statement in the body of this function.
}
Q2: Closure Curry!
Write a function closureCurry that will curry any (Int, Int) > Int function using closures.
closureCurry(add)(1)(2) is equal to 3
closureCurry(multiply)(1)(2) is equal to 2
/**
Return a curried version of twoParamFunc.
 Parameter twoParamFunc: A math function that takes in two Ints.
 Returns: a curried version of twoParamFunc.
*/
func closureCurry(twoParamFunc: @escaping ((Int, Int) > Int)) > ((Int) > ((Int) > Int)){
return ___ // You shall only have this one return statement in this function!
}
You’re done! Upload your screenshot here!
Upload files






If you encounter any issues, please contact me through Wechat, thanks!