object Tails { // Finds all 'tails' of a list def tails[A](xs: List[A]): List[List[A]] = xs match { case Nil => Nil :: Nil // The as-pattern is denoted here using @ // <identifier> @ <expression> reads 'identifier as expression' // so this case match reads 'xxs as _ :: xs' case (xxs @ _ :: xs) => xxs :: tails(xs) } def main(args: Array[String]) = { // List(List(1, 2, 3, 4, 5), List(2, 3, 4, 5), List(3, 4, 5), List(4, 5), List(5), List()) println(tails(List(1, 2, 3, 4, 5))) } }