Recursion 我的Scheme函数:如何使其递归

Recursion 我的Scheme函数:如何使其递归,recursion,scheme,racket,Recursion,Scheme,Racket,因此,在下面提供的scheme函数中,我需要生成一个结构列表。到目前为止,我已经创建了两个助手函数:一个调用从1到x的数字列表。另一个调用对应于x的结构 例如: (helper1 10) -> (list 1 2 3 4 5 6 7 8 9 10) (helper2 1) -> (make-person 0 1) (helper2 2) -> (make-person 1 2) (helper2 3) -> (make-person 2 3) etc...

因此,在下面提供的scheme函数中,我需要生成一个结构列表。到目前为止,我已经创建了两个助手函数:一个调用从1到x的数字列表。另一个调用对应于x的结构

例如:

(helper1 10) -> (list 1 2 3 4 5 6 7 8 9 10)

(helper2 1) -> (make-person 0 1) 

(helper2 2) -> (make-person 1 2) 

(helper2 3) -> (make-person 2 3) etc... 
如何使我的主函数调用helper1列表,其中每个元素都被替换为其相应的结构

注意:我的主函数必须是递归的,并且它必须生成一个结构列表

(define (main-function x)
  (main-function-helper (helper1 x)))

(define (main-function-helper l)
  (cond
    [(empty? l) l]
    [else (cons (helper2 (first l))
                (main-function-helper (rest l)))]))
到目前为止,我的主要职能是:

(define (main-function x)
 (cond
 [(zero? x) empty]
  [else 
     ...]))

另外,我正在用缩写词列表给初学者写作

您需要构建一个全新的列表,而不是用结构替换每个元素。这是最终结果的外观,但不是函数的工作方式

基本上,如果您的
主函数
接受一个数字,那么您需要创建一个实际执行递归的助手函数,在这里您将传入调用
(helper1 x)
的结果

然后,在递归中,您只是用相应的结构重新构造一个列表

(define (main-function x)
  (main-function-helper (helper1 x)))

(define (main-function-helper l)
  (cond
    [(empty? l) l]
    [else (cons (helper2 (first l))
                (main-function-helper (rest l)))]))

另一个更有意义的选择是永远不要创建中间列表:

(define (main-function x)
  (main-function-helper 1 x))

(define (main-function-helper counter max)
  (cond
    [(= counter max) (cons (helper2 counter) empty)]
    [else (cons (helper2 counter)
                (main-function-helper (add1 counter) max))]))