Scheme/RacketR5RS。如何将给定列表和值输入放入嵌套函数列表中

Scheme/Racket R5RS. How to make a given list and a value input, into a nested list of functions

很抱歉,如果这是一个非常新的问题,但我找不到直接的答案。我的任务是定义一个链式函数,给出初始值和函数列表 fn
当我打电话给 chain-all 0 (list f g h)
输出应该是 (f (g (h 0)))
我目前离开了,但到目前为止我有

(define (chain init fns)    
  (if (null? fns) init  
      (cons (car fns) (chain init (cdr fns)))))  

你快到了。您使用 cons 构建列表,因此它是扁平的。你想要嵌套列表,所以改变:

(cons (car fns) (chain init (cdr fns)))))

(list (car fns) (chain init (cdr fns)))))

另外请记住,当您出于测试目的从 REPL 评估此函数时,您可能应该使用 quote 而不是 list:

> (chain 0 `(f g h))