Write a function (transform n fun x) that takes an integer n, a function fun mapping one argument y to fun(y), and an argument x for the function fun. (transform n fun x) returns a list (x, fun(x), fun2(x), …, fun(n-1)(x)). For example, (transform 3 square 2) returns (2 4 16) since the list of squares of the previous element is (2 22 42). The implementation uses helper as a helper function. Complete the following helper procedure. (define (transform n fun x) (define (helper temp count) (if ; You must fill in this part ) ) (helper x 0 ))
时间: 2023-09-28 09:12:18 浏览: 92
Adaptive XML to relational mapping: an integrated approach
Here is the completed helper procedure:
```
(define (helper temp count)
(if (= count n)
'()
(cons temp
(helper (fun temp) (+ count 1)))))
```
And here is the completed `transform` function:
```
(define (transform n fun x)
(define (helper temp count)
(if (= count n)
'()
(cons temp
(helper (fun temp) (+ count 1)))))
(helper x 0))
```
This function takes in three arguments: `n`, `fun`, and `x`. It defines the helper function `helper`, which takes in two arguments: `temp` and `count`. The `temp` argument is used to keep track of the current value that we are applying the function to, while the `count` argument is used to keep track of how many times we have applied the function so far.
The `helper` function checks whether `count` is equal to `n`. If it is, it returns an empty list, indicating that we have applied the function `n` times and have generated a list of length `n`. Otherwise, it applies the function `fun` to `temp` and adds it to the result of calling `helper` recursively with the new value of `temp` and an incremented `count`.
The `transform` function simply calls `helper` with `x` as the initial value of `temp` and `0` as the initial value of `count`. It then returns the resulting list, which contains `x` followed by the results of applying `fun` to `x` `n-1` times.
阅读全文