the Differences of Fold-left and Fold-right
2009-08-08 14:48
369 查看
Fold-left and Fold-right are two frequently used procedures when processing a list. They have the same declarations, but they have different implementations.
The declarations of Fold-left and Fold-right are followed. They combines all of the elements of the-list using the binary operation procedure. If the procedure obeys the associative-law, the fold-right and fold-left will get the same result. If (procedure a (procedure b c)) is equivalent to (procedure (procedure a b) c), then we can say the procedure obeys the associative-law.
Even though fold-left and fold-right behave the same in some cases, the internal calculating process are not the same for such cases. We should know the difference. In essence, fold-left is a iterative process, whereas fold-right is a recursive process.
The mimic implementations for fold-left and fold-right are followed. It is not difficult to make the conclusion.
The declarations of Fold-left and Fold-right are followed. They combines all of the elements of the-list using the binary operation procedure. If the procedure obeys the associative-law, the fold-right and fold-left will get the same result. If (procedure a (procedure b c)) is equivalent to (procedure (procedure a b) c), then we can say the procedure obeys the associative-law.
fold-right procedure initial the-list fold-left procedure initial the-list
Even though fold-left and fold-right behave the same in some cases, the internal calculating process are not the same for such cases. We should know the difference. In essence, fold-left is a iterative process, whereas fold-right is a recursive process.
The mimic implementations for fold-left and fold-right are followed. It is not difficult to make the conclusion.
(define (fold-left op initial sequence) (define (iter result rest) (if (null? rest) result (iter (op result (car rest)) (cdr rest)))) (iter initial sequence)) (define (fold-right2 op initial sequence) (if (null? sequence) initial (op (car sequence) (fold-right2 op initial (cdr sequence)))))
相关文章推荐
- 递归交换二叉树左右子树:BinaryTree:Exchange the left child and right child of a binary tree
- In SQL, what is the difference between a left join and a left outer join?
- The difference of SE Detail UI between 2.4 and 4.0
- travel the binary tree by level 5 ( from down to top and from left to right every level )
- the differences of DataRelation class between 1.1 and 2.0
- The "right-left" Rule of C Declaration (zz from ucsd)
- String and some of the difference between StringBuffer
- Project Manager - the Differences of the Role between Western Countries Company and Chinese Company
- CareerCup View from the left and the right
- What's the difference of name scope and a variable scope in tensorflow?
- Is punishment necessary to help children learn the difference between right and wrong?
- The Difference of Looking for Job and Waiting for Job
- the difference of ViewResult and ActionResult
- The difference of "apt-get update" and "apt-get upgrade"
- the item width must be less than the width of the UICollectionView minus the section insets left and
- the difference between ClientID and ID of web control
- The differences of Iterator and Iterable interface
- About the difference of href='javascript:void(0)' and href=‘#’
- The difference of typeof and GetType method
- 非递归交换二叉树左右子树:BinaryTree:Exchange the left child and the right child without recursive method