Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在Scheme中获取树中元素的总和_List_Scheme_Lisp_Racket - Fatal编程技术网

List 在Scheme中获取树中元素的总和

List 在Scheme中获取树中元素的总和,list,scheme,lisp,racket,List,Scheme,Lisp,Racket,编写一个过程,(fold right tree op id tree),它使用op将树的叶子聚集在一起,类似于在列表上向右折叠。所以如果树有价值 (((1 2) 3) (4 (5 6)) 7 (8 9 10)) 然后 它的价值是55 --我想定义对树中所有元素求和的代码 ;;---------- (#%require racket) (define nil empty) (define (reduce op init lst) (if

编写一个过程,
(fold right tree op id tree)
,它使用op将树的叶子聚集在一起,类似于在列表上向右折叠。所以如果树有价值

(((1 2)
  3)
 (4
  (5 6))
 7
 (8 9 10))
然后

它的价值是55

--我想定义对树中所有元素求和的代码

 ;;----------

    (#%require racket)
    (define nil empty) 


    (define (reduce op init lst)
      (if (null? lst)
          init
          (op (car lst)
              (reduce op init (cdr lst)))))

    (define fold-right reduce)


    (define (sum-list lst)
      (if (null? lst) 0
          (+ (car lst) (sum-list (cdr lst)))
        ))

(define (leaf? x)
  (not (pair? x)))


    (define (fold-right-tree op init tree)
        (lambda (tree)
          (if (null? tree)
              0
              (if (leaf? tree)
                  (sum-list (list tree))
                  (fold-right op init (map fold-right-tree op init tree))))))


    (fold-right-tree (lambda (x,y) (+ x y)) 0 '((1) (2 3 4) (((5)))) )
输出应返回树元素的总和,但 返回
#

我的问题是什么

我也试过这一次,但这次我在映射时出错了

(define (fold-right-tree op init tree)
      (if (null? tree)
          0
          (if (leaf? tree)
              (fold-right op init (list tree))
              (+ (fold-right-tree op init (map car tree)) (fold-right-tree op init (map cdr tree))))))


(fold-right-tree sum 0 '((1) (2 3 4) (((5)))) )

根据问题说明,您可以简单地获取树的所有叶子(使用展平功能),然后应用相关的折叠操作,如:

(define (fold-right-tree op id tree)
  (foldr op id (flatten tree)))

(fold-right-tree + 0 '((1) (2 3 4) (((5)))))  ; => 15

这已经在Dr.Racket中进行了测试。

使用树的合适访问器和谓词(
叶?
空树?
左子树
右子树
),那么显而易见的定义是:

(define (fold-right-tree op accum tree)
  (cond
    [(empty-tree? tree)
     accum]
    [(leaf? tree)
     (op accum tree)]
    [else (fold-right-tree op
                           (fold-right-tree op accum (right-subtree tree))
                           (left-subtree tree))]))
这样做的优点是它对树表示完全不可知:它只知道访问器的名称。事实上,你可以让它成为真正的不可知论:

(define (fold-right-tree op init tree
                         #:empty-tree? (empty? empty-tree?)
                         #:leaf? (lief? leaf?)
                         #:left-subtree (left left-subtree)
                         #:right-subtree (right right-subtree))
  (let frt ([a init] [t tree])
    (cond
      [(empty? t) a]
      [(lief? t) (op a t)]
      [else (frt (frt a (right t)) (left t))])))
现在它将遍历任何类型的二叉树


以下是事实上由树木组成的树木的合适定义:

;;; Tree abstraction
;;;
(define (leaf? treeish)
  (not (pair? treeish)))

(define empty-tree? null?)
(define left-subtree car)
(define right-subtree cdr)

(define cons-tree cons)
(define empty-tree '())

(define (->tree listy
                #:empty-tree (empty empty-tree)
                #:cons-tree (ctree cons-tree))
  ;; turn a cons tree into a tree
  (cond
    [(null? listy) empty]
    [(pair? listy) (ctree (->tree (car listy))
                          (->tree (cdr listy)))]
    [else listy]))

我一下子就看到一只虫子。如果
tree
null?
,为什么忽略调用者的
init
值并返回
0
?如果它们在繁殖呢?或者,如果他们通过一些格式化函数来减少字符串呢?关于您的硬编码
+
的相同评论;您必须使用呼叫者的
op
;;; Tree abstraction
;;;
(define (leaf? treeish)
  (not (pair? treeish)))

(define empty-tree? null?)
(define left-subtree car)
(define right-subtree cdr)

(define cons-tree cons)
(define empty-tree '())

(define (->tree listy
                #:empty-tree (empty empty-tree)
                #:cons-tree (ctree cons-tree))
  ;; turn a cons tree into a tree
  (cond
    [(null? listy) empty]
    [(pair? listy) (ctree (->tree (car listy))
                          (->tree (cdr listy)))]
    [else listy]))