Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 将列表组合推广到N个列表_List_Scala_Recursion_Combinatorics_Combinators - Fatal编程技术网

List 将列表组合推广到N个列表

List 将列表组合推广到N个列表,list,scala,recursion,combinatorics,combinators,List,Scala,Recursion,Combinatorics,Combinators,在Scala中,生成已知数量的列表的组合非常简单。您可以使用以下两种方法进行理解: for { elem1 <- list1 elem2 <- list2 } yield List(elem1, elem2) 在接下来的套件中,我想从N个列表中创建元素的组合(N在运行时是已知的)。在combinators示例中,3个列表为: list1.flatMap( elem1 => list2.flatMap(elem2 => list3.map(elem3 =>

在Scala中,生成已知数量的列表的组合非常简单。您可以使用以下两种方法进行理解:

for {
   elem1 <- list1
   elem2 <- list2 } yield List(elem1, elem2)
在接下来的套件中,我想从N个列表中创建元素的组合(N在运行时是已知的)。在combinators示例中,3个列表为:

list1.flatMap( elem1 => list2.flatMap(elem2 => list3.map(elem3 => List(elem1,elem2,elem3)))
所以我看到了这个模式,我知道那里有一个递归,但我一直在努力确定它

def combinations[T](lists:List[List[T]]): List[List[T]] = ???
有什么想法吗?

还有一种方法:

def combinationList[T](ls:List[List[T]]):List[List[T]] = ls match {
     case Nil => Nil::Nil
     case head :: tail => val rec = combinationList[T](tail)
                          rec.flatMap(r => head.map(t => t::r))
}


scala> val l = List(List(1,2,3,4),List('a,'b,'c),List("x","y"))
l: List[List[Any]] = List(List(1, 2, 3, 4), List('a, 'b, 'c), List(x, y))

scala> combinationList(l)
res5: List[List[Any]] = List(List(1, 'a, x), List(2, 'a, x), List(3, 'a, x),
  List(4, 'a, x), List(1, 'b, x), List(2, 'b, x), List(3, 'b, x), List(4, 'b, x),
  List(1, 'c, x), List(2, 'c, x), List(3, 'c, x), List(4, 'c, x), List(1, 'a, y),
  List(2, 'a, y), List(3, 'a, y), List(4, 'a, y), List(1, 'b, y), List(2, 'b, y),
  List(3, 'b, y), List(4, 'b, y), List(1, 'c, y), List(2, 'c, y), List(3, 'c, y),
  List(4, 'c, y))
def merge[T](a: List[List[T]],b:List[T]) = a match {
    case List() => for(i <- b) yield List(i) 
    case xs => for{ x <- xs; y<- b } yield y ::x 
}

scala> def com[T](ls: List[List[T]]) =  ls.foldLeft(List(List[T]()))((l,x) => merge(l,x))

scala> val l = List(List(1,2,3,4),List('a,'b,'c),List("x","y"))
l: List[List[Any]] = List(List(1, 2, 3, 4), List('a, 'b, 'c), List(x, y))

scala> com(l)
res1: List[List[Any]] = List(List(x, 'a, 1), List(y, 'a, 1), List(x, 'b, 1), Lis
t(y, 'b, 1), List(x, 'c, 1), List(y, 'c, 1), List(x, 'a, 2), List(y, 'a, 2), Lis
t(x, 'b, 2), List(y, 'b, 2), List(x, 'c, 2), List(y, 'c, 2), List(x, 'a, 3), Lis
t(y, 'a, 3), List(x, 'b, 3), List(y, 'b, 3), List(x, 'c, 3), List(y, 'c, 3), Lis
t(x, 'a, 4), List(y, 'a, 4), List(x, 'b, 4), List(y, 'b, 4), List(x, 'c, 4), Lis
t(y, 'c, 4))
def merge[T](a:List[List[T]],b:List[T])=匹配项{
案例列表()=>for(i代表{x merge(l,x))
scala>val l=List(List(1,2,3,4),List('a,'b,'c),List('x,'y'))
l:List[List[Any]=List(List(List(1,2,3,4),List('a,'b,'c),List(x,y))
scala>com(l)
res1:List[List[Any]=List(List(x,'a,1),List(y,'a,1),List(x,'b,1),Lis
t(y,'b,1),List(x,'c,1),List(y,'c,1),List(x,'a,2),List(y,'a,2),Lis
t(x,'b,2),List(y,'b,2),List(x,'c,2),List(y,'c,2),List(x,'a,3),Lis
t(y,'a,3),List(x,'b,3),List(y,'b,3),List(x,'c,3),List(y,'c,3),Lis
t(x,'a,4),List(y,'a,4),List(x,'b,4),List(y,'b,4),List(x,'c,4),Lis
t(y,'c,4))

还有一个解决方案,非常类似于公认的答案,但我认为这更具可读性:

def helper[T](x: List[List[T]]): List[List[T]] = {
  x match {
    case Nil => List(Nil)
    case head :: tail => {
        for (
          n <- head;
          t <- helper(tail)
        ) yield n :: t
      }
  }
}
def helper[T](x:List[List[T]]:List[List[T]={
x匹配{
案例无=>列表(无)
案例头部::尾部=>{
为了(

n为什么-1.任何解释???>。如果您允许自己
scalaz
,似乎可以
list1.replicItem[List](n)
其中
n
是您想要的值的数量。
def helper[T](x: List[List[T]]): List[List[T]] = {
  x match {
    case Nil => List(Nil)
    case head :: tail => {
        for (
          n <- head;
          t <- helper(tail)
        ) yield n :: t
      }
  }
}