October 12, 2009

A Solution to the Set Combinations Problem

def combine[T](sets: List[List[T]]): List[List[T]] = sets match {
  case List(l1, l2) => l1.flatMap(i1 => l2.map(List(i1, _)))
  case head :: tail => combine(tail).flatMap(set => head.map(_ :: set))
  case _ => sets
}

blog comments powered by Disqus
9:58pm  |   permalink
FILED UNDER: scala recursion