Skip to content

Instantly share code, notes, and snippets.

@AndrewSouthpaw
Last active December 26, 2015 06:38
Show Gist options
  • Save AndrewSouthpaw/7108896 to your computer and use it in GitHub Desktop.
Save AndrewSouthpaw/7108896 to your computer and use it in GitHub Desktop.
def insert(pair: (Char, Int), xs: List[(Char, Int)]): List[(Char, Int)] = xs match {
case List() => List(pair)
case y :: ys => {
if (pair._1 == y._1) (pair._1, y._2 + 1) :: ys
else if (pair._1 < y._1) pair :: xs
else y :: insert(pair, ys)
}
}
def insert2(x: (Char, Int), xs: List[Leaf]): List[Leaf] = xs match {
case List() => List(Leaf(x._1, x._2))
case y :: ys => {
if (x._2 < y.weight) Leaf(x._1, x._2) :: xs
else y :: insert2(x, ys)
}
}
def insertGeneral[T2](x: [T1], xs: List[T2], f: T1 => List[T2]): List[T2] = xs match {
case Nil => f(x)
case
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment