-1

Below is the problem:

Define a function make_fn_repeater which takes in a one-argument function
f and an integer x. It should return another function which takes in one
argument, another integer. This function returns the result of applying f to
x this number of times.
Make sure to use recursion in your solution, as shown below:


func make_fn_repeater(___________________):
    if _______________________:
          return __________________
    else:
          return __________________
    return ____________________

Sample output:

incr_1 := make_func_repeater(lambda x: x + 1, 1)
incr_1(2) // returns 3
incr_1(5) // returns 6

Below solution is without recursion:

package main

import "fmt"

type fn func(int) int

func makeFnRepeater(f fn, x int) fn {

    return func(y int) int {
        return f(y)
    }

}

func main() {
    inc := makeFnRepeater(func(x int) int { return x + 1 }, 1)
    fmt.Println(inc(2))
    fmt.Println(inc(5))

}

Is it possible to implement solution using recursion? I don't see that

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • "I don't see that" --- every loop can be turned into a recursion. – zerkms Jul 20 '20 at 23:30
  • "Below solution is without recursion:" --- it's wrong, you don't even use the `x` argument of the `makeFnRepeater` anywhere. – zerkms Jul 20 '20 at 23:31
  • @zerkms we don't have iterative solution for this problem, to then convert to recursion – overexchange Jul 20 '20 at 23:33
  • It requires it explicitly in the description: `This function returns the result of applying f to x this **number of times**`. (the task is a bit dummy though) – zerkms Jul 20 '20 at 23:34
  • @zerkms then I did not understand the question on [page 7](https://inst.eecs.berkeley.edu/~cs61a/sp20/disc/disc03.pdf) – overexchange Jul 20 '20 at 23:44
  • Now it's much more clear: `>>> incr_1(2) #same as f(f(x))` --- see this example: you invoke `f` 2 times here. And in your code you unconditionally invoke `f` only once. – zerkms Jul 20 '20 at 23:49

1 Answers1

1

It sounds like you want to do this:

func makeFnRepeater(f fn, x int) fn {
  if x == 1 {
    return f
  } else {
    return func(y int) int {
      return f(makeFnRepeater(f, x - 1)(y))
    }
  }
}

https://play.golang.org/p/YbiSClzipOK

dave
  • 62,300
  • 5
  • 72
  • 93
  • This code is working. Am unable to visualise this code – overexchange Jul 21 '20 at 02:24
  • If you think of `makeFnRepeater(fn, 2)`, it's going to return a function which applies `fn` to `makeFnRepeater(fn, 1)`, so you effectively get `fn(fn(x))`, if you do it with `makeFnRepeater(fn, 3)` you would get `fn(fn(fn(x)))`, etc. – dave Jul 21 '20 at 16:08
  • So is it `f(makeFnRepeater(f, x-1)(y))` or `f(makeFnRepeater(f, x-1))(y)`? – overexchange Jul 21 '20 at 21:47
  • `f(makeFnRepeater(f, x-1)(y))` – dave Jul 21 '20 at 21:51
  • ok.. now I get this: `makeFnRepeater(fn, 3) -> fn(makeFnRepeater(fn, 2)(4)) -> fn(fn(makeFnRepeater(fn, 1)(4))` for `inc(4)` – overexchange Jul 21 '20 at 23:01