続き
サンプルコード
package fpinscala.datastructures sealed trait List[+A] case object Nil extends List[scala.Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A] object List { def sum(ints: List[Int]): Int = ints match { case Nil => 0 case Cons(x, xs) => x + sum(xs) } def product(sd: List[Double]): Double = sd match { case Nil => 1.0 case Cons(0.0, _) => 0.0 case Cons(x, xs) => x * product(xs) } def apply[A](as: A*): List[A] = if (as.isEmpty) Nil else Cons(as.head, apply(as.tail:_*)) }
があるときに、下記の結果はなんだ!?
val x = List(1,2,3,4,5) match { case Cons(x, Cons(2, Cons(3, Cons(4, _)))) => x case Nil => 42 case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y case Cons(h, t) => h + List.sum(t) case _ => 101 }
見たまんま