1
votes

This is what I have for recursive procedure (repeated f n) that applies the function f n times to an argument:

(define (repeated f count)
  (if (= count 1)
      f 
      (lambda (x)
        (f ((repeated f (- count 1)) x)))))

E.g. ((repeated sqr 3) 2) returns 256, i.e. (sqr(sqr(sqr 2))).

But I have no idea how to implement repeated as an iterative process using Racket. Any advice is much obliged.

2

2 Answers

3
votes

A typical solution for converting a recursive process to an iterative process is to enlist the aid of an accumulator.

Any way you slice it, repeated will have to return a procedure. One solution would use a named let inside the returned procedure that iterates n times, keeping track of the results in an accumulator. Here is a version of repeated that returns a unary procedure; note that there is no input validation here, so calls like ((repeated f 0) 'arg) will lead to trouble.

(define (repeated f n)
  (lambda (x)
    (let iter ((n n)
               (acc x))
      (if (= n 1) (f acc)
          (iter (- n 1)
                (f acc))))))

Named let expressions are very handy for things like this, but you could also define a helper procedure to do the same thing. I will leave that solution as an exercise for OP.

scratch.rkt> ((repeated sqr 3) 2)
256
scratch.rkt> ((repeated add1 8) 6)
14
1
votes

I think using for/fold makes a cleaner solution

(define ((repeated f n) x)
  (for/fold ([acc x]) ([i (in-range n)]) (f acc)))

Using it:

> ((repeated sqr 3) 2)
256
> ((repeated add1 8) 6)
14