Scala函数式编程课后习题答案(第三章)(更新ing)
来源:互联网 发布:三网通游戏源码 编辑:程序博客网 时间:2024/06/07 15:55
Scala函数式编程课后习题答案(第三章)(更新ing)
练习3.9
def foldRight[A,B](as:List[A],z:B)(f:(A,B) =>B):B=as match { case Nil =>z case ::(x,xs) => f(x,foldRight(xs,z)(f)) } def length[A](as:List[A]):Int =foldRight(as,0)((_,xs)=>xs+1)
练习3.10 3.13
def foldLeft[A,B](as:List[A],z:B)(f:(B,A)=>B):B = as match { case Nil =>z case ::(x,xs) =>foldLeft(xs,f(z,x))(f) }
练习3.11
def foldLeft[A,B](as:List[A],z:B)(f:(B,A)=>B):B = as match { case Nil =>z case ::(x,xs) =>foldLeft(xs,f(z,x))(f) } def sum3(ns:List[Int])=foldLeft(ns,0)(_+_) def product3(ns:List[Double])=foldLeft(ns,1.0)(_*_)
练习3.14
def foldLeft[A,B](as:List[A],z:B)(f:(B,A)=>B):B = as match { case Nil =>z case ::(x,xs) =>foldLeft(xs,f(z,x))(f) } def append[A](as:List[A],a:List[A]):List[A] = foldLeft(as.reverse,a){(x,xs)=> ::(xs,x)}
练习3.15
def foldLeft[A,B](as:List[A],z:B)(f:(B,A)=>B):B = as match { case Nil =>z case ::(x,xs) =>foldLeft(xs,f(z,x))(f) } def append[A](as:List[A],a:List[A]):List[A] = foldLeft(as.reverse,a){(x,xs)=> ::(xs,x)} def concat[A](ll:List[List[A]]):List[A]=foldLeft(ll,Nil:List[A]){append}
练习3.16 3.17 3.18
def foldLeft[A,B](as:List[A],z:B)(f:(B,A)=>B):B = as match { case Nil =>z case ::(x,xs) =>foldLeft(xs,f(z,x))(f) } def map_2[A,B](l: List[A])(f: A => B): List[B] = foldLeft(l.reverse,Nil: List[B]){(h,acc) => ::(f(acc),h)}
练习3.19
def foldLeft[A,B](as:List[A],z:B)(f:(B,A)=>B):B = as match { case Nil =>z case ::(x,xs) =>foldLeft(xs,f(z,x))(f) } //过滤 def filter_2[A](as: List[A])(f: A => Boolean):List[A]= foldLeft(as.reverse,Nil: List[A]){(h,acc) =>if(f(acc)) ::(acc,h) else h}
练习3.20
def foldLeft[A,B](as:List[A],z:B)(f:(B,A)=>B):B = as match { case Nil =>z case ::(x,xs) =>foldLeft(xs,f(z,x))(f) } //过滤 def flatMap_2[A,B](l: List[A])(f: A => List[B]): List[B] = concat(map_2(l)(f))
练习3.24
//前一个List是否包括后一个List中的元素 def compareList[A](as:List[A],a:List[A]):Boolean = (as,a) match{ case (_,Nil) => true case (::(x1,xs1),::(x2,xs2)) if(x1==x2) => compareList(xs1,xs2) case (::(x1,xs1),::(x2,xs2)) if(x1!=x2) => compareList(xs1,::(x2,xs2)) case _ =>false }
练习3.25 3.26 3.27 3.28 3.29
sealed trait Tree[+A] { def size: Int = this match { case Leaf(_) => 1 case Branch(l,r) => 1 + l.size + r.size } def maximum: Int = this match { case Leaf(a:Int) => a case Branch(l,r) => l.maximum max r.maximum } def depth:Int = this match { case Leaf(_) =>0 case Branch(l,r) => 1 +(l.depth max r.depth) } def map(f:Int=>Int):Tree[Int] = this match { case Leaf(x:Int) => Leaf(f(x)) case Branch(l,r) => Branch(l.map(f),r.map(f)) } def fold[B](f:A=>B)(g:(B,B)=>B):B =this match { case Leaf(n) => f(n) case Branch(l,r) => g(l.fold(f)(g),r.fold(f)(g)) } def sizeByfold = fold(_=>1)(1+_+_) def maximumByfold(ll:Tree[Int]) = ll.fold(a =>a)((l,r)=>0+(l max r)) def depthByfold = fold(_=>0)((l,r)=>1+(l max r)) //def mapByfold[C](f:C=>C) = fold(a=>f(a))((l,r)=>) } case class Leaf[A](value: A) extends Tree[A] case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]
0 0
- Scala函数式编程课后习题答案(第三章)(更新ing)
- Scala函数式编程课后习题答案(第四章)(更新ing)
- Scala函数式编程课后习题答案(第五章)
- Scala函数式编程课后习题答案(第六章)
- 《java8函数式编程》第三章课后习题
- 模板元编程 第三章课后练习(非答案)
- 《C语言程序设计教程》(主编黄迪明、余勤)第三章课后习题答案
- 快学Scala 第三章习题答案
- 快学Scala 第三章习题答案
- IBM-PC汇编语言程序设计(第二版)课后习题答案(清华大学出版社)(沈美明,温冬蝉著)第三章答案
- 快学 scala 第二章课后习题答案
- 数据挖掘概念与技术(原书第三版)范明 孟小峰译-----第三章课后习题答案
- Python核心编程(第二版)第六章课后习题答案
- 模板元编程第六章课后习题(非答案)20151206
- 编程珠玑第三章课后习题
- 快学Scala习题答案(第12章高阶函数)
- 数据库系统概论课后习题答案第三章
- C++Primer 中文版 第五版 第三章课后习题答案
- api level 对照
- 防止头文件中重复定义的小方法
- java基础---->Base64算法的使用
- 深入理解SELinux/SEAndroid(第一部分)
- Centos7 install KVM
- Scala函数式编程课后习题答案(第三章)(更新ing)
- BlockingQueue队列实现线程池
- 第八届蓝桥杯 C/C++ C组第9题 拉马车
- Unity获取安卓手机运营商信息
- 今天入职新公司。
- CentOS 中 YUM 安装桌面环境
- django 使用 PIL 压缩图片
- MySQL数据的主从复制、半同步复制和主主复制详解
- 欢迎使用CSDN-markdown编辑器