泛函编程(7)-数据结构-List-折叠算法

简介:

  折叠算法是List的典型算法。通过折叠算法可以实现众多函数组合(function composition)。所以折叠算法也是泛函编程里的基本组件(function combinator)。了解折叠算法的原理对了解泛函组合有着至关紧要的帮助。折叠算法又可分右折叠和左折叠。我们先从右折叠(foldRight)开始:

 foldRightfoldRight

从以上两图示可以得出对List(a,b,c)的右折叠算法:op(a,op(b,op(c,z))) 可以看出括号是从右开始的。计算方式如图二:op(a,sub), sub是重复子树,可以肯定要用递归算法。这里z代表了一个起始值。我们现在可以推算出foldRight的函数款式(function signature)了:


1       def foldRight[A,B](l: List[A], z: B)(op: (A,B) => B): B = l match {
2           case Nil => z
3           case Cons(h,t) => op(h,foldRight(t,z)(f))
4       }

注意foldRight不是一个尾递归算法(tail recursive)。我们试着对一个List(1,2,3)进行操作,先来个加法: 


1 foldRight(List(1,2,3),0)((x,y) => x + y)          //> res13: Int = 6
2 foldRight(List(1,2,3),0){_ + _}                   //> res14: Int = 6

我们可以用”等量替换“方法简约:


1  // (List(x1,x2,x3...x{n-1}, xn) foldRight acc) op => x1 op (...(xn op acc)...)
2  // foldRight(Cons(1,Cons(2,Cons(3,Nil))), 0) {_ + _}
3  // 1 + foldRight(Cons(2,Cons(3,Nil)), 0) {_ + _}
4  // 1 + (2 + foldRight(Cons(3,Nil), 0) {_ + _})
5  // 1 + (2 + (3 + foldRight(Nil, 0) {_ + _}))
6  // 1 + (2 + (3 + 0)) = 6

1 foldRight(List(1,2,3),1){_ * _}                   //> res16: Int = 6
2 foldRight(List(1,2,3),Nil:List[Int]) { (a,b) => Cons(a+10,b) }
                                                    //> res17: ch3.list.List[Int] = Cons(11,Cons(12,Cons(13,Nil)))

注意以上的起始值1和Nil:List[Int]。z的类型可以不是A,所以op的结果也有可能不是A类型,但在以上的加法和乘法的例子里z都是Int类型的。但在List重构例子里z是List[Int]类型,所以op的结果也是List[Int]类型的,这点要特别注意。

再来看看左折叠算法:

foldLeftfoldLeft

从以上图示分析,左折叠算法就是所有List元素对z的操作op。从图二可见,op对z,a操作后op的结果再作为z与b再进行op操作,如此循环。看来又是一个递归算法,而z就是一个用op累积的值了:op(op(op(z,a),b),c)。左折叠算法的括号是从左边开始的。来看看foldLeft的实现:


1       def foldLeft[A,B](l: List[A], acc: B)(op: (B,A) => B): B = l match {
2           case Nil => acc
3           case Cons(h,t) => foldLeft(t,op(acc,h))(op)
4       }

注意z (zero) 变成了 acc (accumulator),op: (B,A) = B, 和foldRight的op函数入参顺序是颠倒的。foldLeft是个尾递归方法。


1 foldLeft(List(1,2,3),0)((b,a) => a + b)           //> res18: Int = 6
2 foldLeft(List(1,2,3),0){_ + _}                    //> res19: Int = 6
3 foldLeft(List(1,2,3),1)((b,a) => a * b)           //> res20: Int = 6
4 foldLeft(List(1,2,3),1){_ * _}                    //> res21: Int = 6
5 foldLeft(List(1,2,3),Nil:List[Int]) { (b,a) => Cons(a+10,b) }
6                                                   //> res22: ch3.list.List[Int] = Cons(13,Cons(12,Cons(11,Nil)))

以上加法和乘法的累积值acc都是A类型,但注意List重构的acc是List[Int]类型的,这个时候op入参的位置就很重要了。再注意一下,foldLeft重构的List的元素排列是反向的Cons(13,Cons(12,Cons(11,Nil))。我们还是可以用“等量替换”方法进行简约:


1 // (List(x1,x2,x3...x{n-1}, xn) foldLeft acc) op => (...(acc op x1) op x2)...) op x{n-1}) op xn
2  // foldLeft(Cons(1,Cons(2,Cons(3,Nil))), 0) {_ + _}
3  // foldLeft(Cons(2,Cons(3,Nil)), (0 + 1)) {_ + _}
4  // foldLeft(Cons(3,Nil), ((0 + 1) + 2)) {_ + _}
5  // foldLeft(Nil, (((0 + 1) + 2) + 3)) {_ + _}
6  // (((0 + 1) + 2) + 3) + 0 = 6

除foldRight,foldLeft之外,折叠算法还包括了:reduceRight,reduceLeft,scanRight,scanLeft。

 reduceLeftreduceRight

reduceLeft是以第一个,reduceRight是以最后一个List元素作为起始值的折叠算法,没有单独的起始值:


1       def reduceLeft[A](l: List[A])(op: (A,A) => A): A = l match {
2           case Nil => sys.error("Empty list!")
3           case Cons(h,t) => foldLeft(t,h)(op)
4       }
5       def reduceRight[A](l: List[A])(op: (A,A) => A): A = l match {
6           case Cons(h,Nil) => h
7           case Cons(h,t) => op(h,reduceRight(t)(op))
8       }

1  reduceLeft(List(1,2,3)) {_ + _}                  //> res23: Int = 6
2  reduceRight(List(1,2,3)) {_ + _}                 //> res24: Int = 6

scanLeft, scanRight 分别把每次op的结果插入新产生的List作为返回结果。

 先实现scanLeft:


1        def scanLeft[A](l: List[A],z: A)(op: (A,A) => A): List[A] = l match {
2            case Nil => Cons(z,Nil)
3            case Cons(h,t) => Cons(z,scanLeft(t,op(z,h))(op))
4        }

1 scanLeft(List(1,2,3),0) {_ + _}                   //> res25: ch3.list.List[Int] = Cons(0,Cons(1,Cons(3,Cons(6,Nil))))

试试简约:

 1  // (List(x1,x2,x3...x{n-1}, xn) scanLeft acc) op => (...(acc op x1) op x2)...) op x{n-1}) op xn
 2  // scanLeft(Cons(1,Cons(2,Cons(3,Nil))), 0) {_ + _}
 3  // Cons(0,scanLeft(Cons(1,Cons(2,Cons(3,Nil))), 0) {_ + _})
 4  // Cons(0,Cons((0 + 1), scanLeft(Cons(2,Cons(3,Nil)), (0 + 1)) {_ + _}))
 5  // ==> Cons(0,Cons(1,scanLeft(Cons(2,Cons(3,Nil)), 1) {_ + _}))
 6  // Cons(0,Cons(1,Cons(2 + 1,scanLeft(Cons(3,Nil), 1 + 2) {_ + _})))
 7  // ==> Cons(0,Cons(1,Cons(3,scanLeft(Cons(3,Nil), 3) {_ + _})))
 8  // Cons(0,Cons(1,Cons(3,Cons(3 + 3,foldLeft(Nil, 3 + 3) {_ + _}))))
 9  // ==> Cons(0,Cons(1,Cons(3,Cons(6,foldLeft(Nil, 6) {_ + _}))))
10  // Cons(0,Cons(1,Cons(3,Cons(6,Nil))))

再实现scanRight:


 1     def reverse[A](l: List[A]): List[A] = foldLeft(l,Nil:List[A]){(acc,h) => Cons(h,acc)}
 2        
 3        def scanRight[A](l: List[A],z: A)(op: (A,A) => A): List[A] =  {
 4                 var scanned = List(z)
 5                 var acc = z
 6                 var ll = reverse(l)
 7                 var x = z
 8                 while (
 9                 ll match {
10                              case Nil => false
11                              case Cons(h,t) => { x = h; ll = t; true }
12                 }
13             ) {
14                         acc = op(acc,x)
15                            scanned = Cons(acc,scanned)
16                 }
17          scanned
18       }

实在没能想出用递归算法实现scanRight的方法,只能用while loop来解决了。注意虽然使用了临时变量,但这些变量都是本地封闭的,所以scanRight还是纯函数。scanRight元素遍历(traverse)顺序是反向的,所以用reverse函数把List(1,2,3)先变成List(3,2,1)。


1 scanRight(List(1,2,3),0) {_ + _}                  //> res26: ch3.list.List[Int] = Cons(6,Cons(5,Cons(3,Cons(0,Nil))))

注意scanRight和scanLeft的结果不同。这是因为算法不同:元素遍历(traverse)顺序不同。

下面开始示范一下折叠算法作为基本组件(combinator)来实现一些函数功能:

上次实现了函数++,即append。我们同样可以用foldLeft和foldRight来实现:


1       def appendByFoldRight[A](l1: List[A], l2: List[A]): List[A] = foldRight(l1,l2){(h,acc) => Cons(h,acc)}
2       def appendByFoldLeft[A](l1: List[A], l2: List[A]): List[A] = foldLeft(reverse(l1),l2){(acc,h) => Cons(h,acc)}

1 appendByFoldLeft(List(1,2),List(3,4))             //> res27: ch3.list.List[Int] = Cons(1,Cons(2,Cons(3,Cons(4,Nil))))
2 appendByFoldRight(List(1,2),List(3,4))            //> res28: ch3.list.List[Int] = Cons(1,Cons(2,Cons(3,Cons(4,Nil))))

由于append的功能是将两个List拼接起来,必须保证最终结果List元素的顺序。所以在appendByFoldLeft里使用了reverse。再注意foldLeft和foldRight在op参数位置是相反的。

之前递归算法实现的函数有些是可以用折叠算法实现的:


1       def map_1[A,B](l: List[A])(f: A => B): List[B] = foldRight(l,Nil: List[B]){(h,acc) => Cons(f(h),acc)}

1       def filter_1[A](l: List[A])(f: A => Boolean): List[A] = foldRight(l,Nil: List[A]){(h,acc) => if (f(h)) Cons(h,acc) else acc }
2       def flatMap_1[A,B](l: List[A])(f: A => List[B]): List[B] = foldRight(l,Nil: List[B]){(h,acc) => appendByFoldRight(f(h),acc)}

1       def lengthByFoldRight[A](l: List[A]): Int = foldRight(l,0){(_,acc) => acc + 1 }
2       def lengthByFoldLeft[A](l: List[A]): Int = foldLeft(l,0){(acc,_) => acc + 1 }

还有些比较间接的:

1     def conCat[A](ll: List[List[A]]): List[A] = foldRight(ll,Nil: List[A]){appendByFoldRight}


这个函数可以用来实现flatMap:

1      def flatMap_1[A,B](l: List[A])(f: A => List[B]): List[B] = conCat(map(l)(f))

如果理解以上函数实现方式有困难时可以先从类型匹配上下手,或者试着用“等量替换”方法简约跟踪一下。

相关文章
|
2天前
|
机器学习/深度学习 存储 算法
数据结构与算法 动态规划(启发式搜索、遗传算法、强化学习待完善)
数据结构与算法 动态规划(启发式搜索、遗传算法、强化学习待完善)
8 1
|
5天前
|
搜索推荐 C语言
【C语言/数据结构】排序(归并排序|计数排序|排序算法复杂度)
【C语言/数据结构】排序(归并排序|计数排序|排序算法复杂度)
11 0
|
5天前
|
存储 算法
Leetcode 30天高效刷数据结构和算法 Day1 两数之和 —— 无序数组
给定一个无序整数数组和目标值,找出数组中和为目标值的两个数的下标。要求不重复且可按任意顺序返回。示例:输入nums = [2,7,11,15], target = 9,输出[0,1]。暴力解法时间复杂度O(n²),优化解法利用哈希表实现,时间复杂度O(n)。
16 0
|
12天前
|
存储 机器学习/深度学习 算法
|
16天前
|
存储 NoSQL Redis
Redis入门到通关之Redis数据结构-List篇
Redis入门到通关之Redis数据结构-List篇
33 1
|
16天前
|
存储 算法 Python
程序设计的艺术:算法与数据结构的魅力
程序设计的艺术:算法与数据结构的魅力
8 0
|
16天前
|
存储 算法
数据结构开篇(普普通通浅浅聊数据结构)什么是数据结构 、什么是算法、重要性、如何学好数据结构呢
数据结构开篇(普普通通浅浅聊数据结构)什么是数据结构 、什么是算法、重要性、如何学好数据结构呢
|
24天前
|
存储 人工智能 算法
有哪些数据结构与算法是程序员必须要掌握的?——“数据结构与算法”
有哪些数据结构与算法是程序员必须要掌握的?——“数据结构与算法”
|
26天前
|
存储 算法 Serverless
数据结构期末复习(六)查找算法
数据结构期末复习(六)查找算法
11 0
|
26天前
|
存储 人工智能 算法
数据结构期末复习(1)数据结构和算法 线性表
数据结构期末复习(1)数据结构和算法 线性表
16 0