Recursion 降低球拍的功能?

Recursion 降低球拍的功能?,recursion,functional-programming,scheme,racket,Recursion,Functional Programming,Scheme,Racket,我被这项任务缠住了5天,有人知道我该怎么做吗 给定arity 2的函数和n元素列表,返回所有元素的字符串函数的求值,例如: 给你 (define (reduce func list) (assert (not (null? list))) (if (null? (cdr list)) (car list) (func (car list) (reduce func (cdr list))))) 测试: > (reduce + '(1 2 3 4 5 6 7

我被这项任务缠住了5天,有人知道我该怎么做吗

给定arity 2的函数和
n
元素列表,返回所有元素的字符串函数的求值,例如:

给你

(define (reduce func list)
  (assert (not (null? list)))
  (if (null? (cdr list))
      (car list)
      (func (car list) (reduce func (cdr list)))))
测试:

> (reduce + '(1 2 3 4 5 6 7 8 9 10))
55
> (reduce zip '((1 2 3) (4 5 6) (7 8 9)))
((1 (4 7)) (2 (5 8)) (3 (6 9)))
为完整起见,
zip
(假设两个列表且您的列表长度相同)的实现是:


您可以用以下方式表示:


示例输出似乎不正确,请检查是否正确。另外,发布了
zip
的实现。哎哟,被狙击手击中了。在没有解释的情况下投了反对票——答案正确。放马过来你说得对,答案似乎是正确的。给你我不明白为什么人民党不互相投票。我们这里的观众并不多。你能把
zip
的定义显示出来吗?或者你使用的是内置的?(很明显,它是
(define(zip a b)(map list a b))
,但为了完整起见,它可能应该在答案中)。谢谢。同一个用户问了一个关于实现
zip
的问题。请参见已完成的操作。:)这被认为是家庭作业,所以给出完整的代码是不可取的…:)。。。你的意思是说你在这里使用的邮政编码与你在那里使用的邮政编码相同?不过,在这里也最好包含代码(或其框架)。(?)
> (reduce + '(1 2 3 4 5 6 7 8 9 10))
55
> (reduce zip '((1 2 3) (4 5 6) (7 8 9)))
((1 (4 7)) (2 (5 8)) (3 (6 9)))
(define (zip l1 l2) (map list l1 l2))
(define (reduce f xs)
  (and (not (empty? xs)) (foldl f (first xs) (rest xs))))
(define reduce
  (λ (f init ls)
    (if (empty? ls)
        init
        (reduce f (f init (first ls)) (rest ls)))))