Implement pack and encode

Lecture 5.4 - Higher-Order List Functions (14:53)
In video quiz
11:11 / 14:53

Write a function pack that packs consecutive duplicates of list elements
into sublists. For instance,

  pack(List("a", "a", "a", "b", "c", "c", "a"))

should give

  List(List("a", "a", "a"), List("b"), List("c", "c"), List("a")).

You can use the following template:

  def pack[T](xs: List[T]): List[List[T]] = xs match {
    case Nil      => Nil
    case x :: xs1 => ???
  }

Using pack, write a function encode that produces the run-length
encoding of a list. The idea is to encode n consecutive duplicates of an
element x as a pair (x, n). For instance,

  encode(List("a", "a", "a", "b", "c", "c", "a"))

should give

  List(("a", 3), ("b", 1), ("c", 2), ("a", 1)).