3

找到了一个从 Scala 中的列表列表中创建笛卡尔积的函数。但是,它不是尾递归的,并且不适用于大型列表。不幸的是,在设计时我不知道需要组合多少个列表,所以我认为递归函数是必要的。我正在努力使其尾递归,以便编译器对其进行优化:

def product[T](listOfLists: List[List[T]]): List[List[T]] = listOfLists match {
    case Nil => List(List())
    case xs :: xss => for (y <- xs; ys <- product(xss)) yield y :: ys
}
4

1 回答 1

5

这种方法类似于您的原始方法,除了不是开始和前面并递归下降直到您到达末尾并追加备份,我已经引入了一个累加器,我可以向后移动列表,累积作为我去。

import annotation.tailrec

def product[T](listOfLists: List[List[T]]): List[List[T]] = {
  @tailrec def innerProduct[T](listOfLists: List[List[T]], accum: List[List[T]]): List[List[T]] =
    listOfLists match {
      case Nil => accum
      case xs :: xss => innerProduct(xss, for (y <- xs; a <- accum) yield y :: a)
    }

  innerProduct(listOfLists.reverse, List(Nil))
}

然后:

scala> product(List(List(1,2),List(3,4)))
res0: List[List[Int]] = List(List(1, 3), List(1, 4), List(2, 3), List(2, 4))
于 2012-04-24T00:51:03.313 回答