快学Scala习题解答—第十四章 模式匹配和样例类
没读懂题意。。。。
def swap[S,T](tup: (S,T)) = { tup match { case (a ,b) => (b,a) }}println(swap[String,Int](("1",2)))
def swap(arr: Array[String]) = { arr match { case Array(a,b, ar @ _*) => Array(b,a) ++ ar case _ => arr }}println(swap(Array("1","2","3","4")).mkString)
abstract class Itemcase class Multiple(num : Int,item : Item) extends Itemcase class Article(description : String , price : Double) extends Itemcase class Bundle(description : String , discount : Double , item : Item*) extends Itemobject Test extends App{ def price(it : Item) : Double = it match { case Article(_,p) => p case Bundle(_,disc,its @ _*) => its.map(price _).sum - disc case Multiple(n,it) => n * price(it) } val p = price(Multiple(10,Article("Blackwell Toster",29.95))) println(p)}
* / | \ * 2 * / \ |3 8 5
不过,有些列表元素是数字,而另一些是列表。在Scala中,你不能拥有异构的列表,因此你必须使用List[Any]。编写一个leafSum函数,计算所有叶子节点中的元素之和,用模式匹配来区分数字和列表。
val l: List[Any] = List(List(3, 8), 2, List(5))def leafSum(list: List[Any]): Int = { var total = 0 list.foreach { lst => lst match { case l: List[Any] => total += leafSum(l) case i: Int => total += i } } total}println(leafSum(l))
sealed abstract class BinaryTreecase class Leaf(value : Int) extends BinaryTreecase class Node(left : BinaryTree,right : BinaryTree) extends BinaryTree
编写一个函数计算所有叶子节点中的元素之和。
sealed abstract class BinaryTreecase class Leaf(value : Int) extends BinaryTreecase class Node(left : BinaryTree,right : BinaryTree) extends BinaryTreeval r = Node(Leaf(3),Node(Leaf(3),Leaf(9)))def leafSum(tree: BinaryTree): Int = { tree match { case Node(a,b) => leafSum(a) + leafSum(b) case Leaf(v) => v }}println(leafSum(r))
Node(Node(Leaf(3),Leaf(8)),Leaf(2),Node(Leaf(5)))
sealed abstract class BinaryTreecase class Leaf(value: Int) extends BinaryTreecase class Node(tr: BinaryTree*) extends BinaryTreeobject Test extends App { val r = Node(Node(Leaf(3), Leaf(8)), Leaf(2), Node(Leaf(5))) def leafSum(tree: BinaryTree): Int = { tree match { case Node(r @ _*) => r.map(leafSum).sum case Leaf(v) => v } } println(leafSum(r))}
+ / | \ * 2 - / \ |3 8 5
上面这棵树的值为(3 * 8) + 2 + (-5) = 21
sealed abstract class BinaryTreecase class Leaf(value: Int) extends BinaryTreecase class Node(ch : Char , tr: BinaryTree*) extends BinaryTreeobject Test extends App { val r = Node('+' , Node('*',Leaf(3), Leaf(8)), Leaf(2), Node('-' , Leaf(5))) def eval(tree: BinaryTree): Int = { tree match { case Node(c : Char , r @ _*) => if( c == '+') r.map(eval).sum else if (c == '*') r.map(eval).reduceLeft(_ * _) else r.map(eval).foldLeft(0)(_ - _) case Leaf(v) => v } }println(eval(r))}
val l : List[Option[Int]] = List(Option(-1),None,Option(2))println(l.map(_.getOrElse(0)).sum)
def f(x : Double) = if ( x >= 0) Some(sqrt(x)) else Nonedef g(x : Double) = if ( x != 1) Some( 1 / ( x - 1)) else Noneval h = compose(f,g)
h(2)将得到Some(1),而h(1)和h(0)将得到None
import scala.math.sqrtdef f(x : Double) = if ( x >= 0) Some(sqrt(x)) else Nonedef g(x : Double) = if ( x != 1) Some( 1 / ( x - 1)) else Noneval h = compose(f,g)def compose(f : (Double => Option[Double]), g : (Double => Option[Double])):(Double => Option[Double])={ (x : Double) => if (f(x) == None || g(x) == None) None else g(x)}println(h(2))
Blog URL:http://www.ivanpig.com/blog/?p=513