# 變數綁定（Variable binding）、模式防護（Pattern guard）

``def what(list: List[Int]) = list match {    case List(2, _*)   => list.tail    case List(3, _*)   => list.tail    case _             => Nil}println(what(List(2, 3, 4, 5)))   // List(3, 4, 5)println(what(List(3, 4, 5, 6)))   // List(4, 5, 6)println(what(List(1, 3, 4, 5)))   // List()``

``def what(list: List[Int]) = list match {    case List(2, tail @ _*)   => tail    case List(3, tail @ _*)   => tail    case _                    => Nil}println(what(List(2, 3, 4, 5)))   // List(3, 4, 5)println(what(List(3, 4, 5, 6)))   // List(4, 5, 6)println(what(List(1, 3, 4, 5)))   // List()``

``case class Point(x: Int, y: Int)case class Circle(p: Point, r: Int)case class Cylinder(c: Circle, h: Int)def what(c: Cylinder) = c match {    case Cylinder(circle @ Circle(Point(0, 0), _), _) => circle    case _                                            => null}val cy1 = Cylinder(Circle(Point(0, 0), 10), 10)val cy2 = Cylinder(Circle(Point(0, 0), 20), 20)val cy3 = Cylinder(Circle(Point(1, 1), 20), 20)println(what(cy1))  // Circle(Point(0,0),10)println(what(cy2))  // Circle(Point(0,0),20)println(what(cy3))  // null``

``case class Point(x: Int, y: Int)case class Circle(p: Point, r: Int)case class Cylinder(c: Circle, h: Int)def what(c: Cylinder) = c match {    case Cylinder(Circle(Point(0, 0), _), _) => c.c    case _                                   => null}val cy1 = Cylinder(Circle(Point(0, 0), 10), 10)val cy2 = Cylinder(Circle(Point(0, 0), 20), 20)val cy3 = Cylinder(Circle(Point(1, 1), 20), 20)println(what(cy1))  // Circle(Point(0,0),10)println(what(cy2))  // Circle(Point(0,0),20)println(what(cy3))  // null``

``case class Point(x: Int, y: Int)case class Circle(p: Point, r: Int)def what(c: Circle) = c match {    case Circle(Point(x, y), r) if x == y => r    case _                                => c}println(what(Circle(Point(1, 1), 10)))   // 10println(what(Circle(Point(2, 2), 20)))   // 20println(what(Circle(Point(3, 1), 10)))   // Circle(Point(3,1),10)println(what(Circle(Point(4, 4), 30)))   // 30``

``case class Point(x: Int, y: Int)case class Circle(p: Point, r: Int)def what(c: Circle) = c match {    case Circle(Point(x, y), r) => if(x == y) r else c}println(what(Circle(Point(1, 1), 10)))   // 10println(what(Circle(Point(2, 2), 20)))   // 20println(what(Circle(Point(3, 1), 10)))   // Circle(Point(3,1),10)println(what(Circle(Point(4, 4), 30)))   // 30``