[Scala] Pattern Matching(模式匹配)

时间:2022-12-25 13:57:03

Scala中的match, 比起以往使用的switch-case有著更強大的功能,

1. 傳統方法

def toYesOrNo(choice: Int): String = choice match {
case 1 => "yes"
case 0 => "no"
case _ => "error"
} // toYesOrNo(1)=>"yes"
// toYesOrNo(0)=>"no"
// toYesOrNo(33)=>"error"
 

佔位符“_”表示預設的情形, 若不想使用"_"也可以寫成以下的形式,

def toYesOrNo(choice: Int): String = choice match {
case 1 => "yes"
case 0 => "no"
case whaterver => "whaterver "
}
 
2. 類型模式
 
可以使用保留字match來判斷類型
def f(x: Any): String = x match {
case i:Int => "integer: " + i
case _:Double => "a double"
case s:String => "I want to say " + s
} // f(1) → “integer: 1″Typ
// f(1.0) → “a double”
// f(“hello”) → “I want to say hello”

3. Functional approach to pattern matching

以下是一個Factorial的傳統遞迴方法

def fact(n: Int): Int =
if (n == 0) 1
else n * fact(n - 1)

改以pattern matching來實現, 又會如何呢??

def fact(n: Int): Int = n match {
case 0 => 1
case n => n * fact(n - 1)
}

4. 模式匹配與集合

來試試一個集合加總的遞迴實現, 我們可能會寫出以下的代碼

def length[A](list : List[A]) : Int = {
if (list.isEmpty) 0
else 1 + length(list.tail)
}

看起來沒什麼問題, 但在pattern matching下有更酷的寫法,

def length[A](list : List[A]) : Int = list match {
case _ :: tail => 1 + length(tail)
case Nil => 0
}

"Nil"代表集合為空時,

"_::tailX" 應該被理解成, “a list with whatever head followed by a tail.”我的解釋是能將一個list拆分為list.head與list.tail

接著我們可以來看看多個參數的pattern matching要怎麼做呢??

def parseArgument(arg : String, value: Any) = (arg, value) match {
case ("-l", lang) => setLanguageTo(lang)
case ("-o" | "--optim", n : Int) if ((0 < n) && (n <= 5)) => setOptimizationLevelTo(n)
case ("-o" | "--optim", badLevel) => badOptimizationLevel(badLevel)
case ("-h" | "--help", null) => displayHelp()
case bad => badArgument(bad)
}

在pattern中還有最重要的case class, 下一篇繼續介紹....

參考資料:

Playing with Scala’s pattern matching

A Tour of Scala: Case Classes