Write a program to calculate the run length encoding of list of characters.
For each character find out total number of consecutive occurrences

def encode(xs: List[Char]): List[(Char, Int)] = {
  
  def pack(ys: List[Char]): List[List[Char]] = ys match {
    case Nil => Nil
    case z::zs => {
      val (fl, sl) = ys.span(z.equals(_))
      fl :: pack(sl)
    }
  }
  
  pack(xs).map(list => (list.head, list.size))
  
}

 

val myList = List('a', 'a', 'a', 'c', 'd', 'd', 'e', 'e', 'e', 'f', 'f', 'f', 'f', 'f', 'g')
encode(myList)  // res0: List[(Char, Int)] = List((a,3), (c,1), (d,2), (e,3), (f,5), (g,1))