Starting Files
Download this playground with the problem inside!
The Problem
/// Challenge: Implement a function makeRepeater so that makeRepeater(f: f, n: n)(x) returns f(f(...f(x)...)), where f is applied n times. That is, makeRepeater(f: f, n: n) returns another function that can then be applied to another argument. For example, makeRepeater(f: square, n: 3)(42) evaluates to square(square(square(42))). Yes, it makes sense to apply the function zero times! See if you can figure out a reasonable function to return for that case. Use only closures. The body of makeRepeater should only have one statement.
/**
Returns a function that will take in a number and apply some function f to it n times.
 Parameter f: a function that takes in an integer and returns an integer
 Parameter n: the number of times f will be applied to some integer
 Returns: a function that will take in an integer x and apply f to x n times.
*/
func makeRepeater(f: @escaping (Int) > Int, n: Int) > ((Int) > Int){
return ___ // You should only have this one return statement as the body of this function.
}
makeRepeater(f:{ (x: Int) in return x*x}, n: 3)(2) is equal to ((2^2)^2)^2
makeRepeater(f:{ (x: Int) in return x+1}, n: 3)(2) is equal to ((2+1)+1)+1
makeRepeater(f:{ (x: Int) in return x/2}, n: 3)(2) is equal to ((2/2)/2)/2
Implement a function makeRepeater so that makeRepeater(f: f, n: n)(x) returns f(f(…f(x)…)), where f is applied n times. That is, makeRepeater(f: f, n: n) returns another function that can then be applied to another argument. For example, makeRepeater(f: square, n: 3)(42) evaluates to square(square(square(42))).
Yes, it makes sense to apply the function zero times! See if you can figure out a reasonable function to return for that case.
Use only closures. The body of makeRepeater should only have one statement.
Upload your screenshot here!
Upload files





