为什么cons不是immutable.this.IndexedSeq[List[List[scala.this.Tuple2[scala.this.Char,scala.this.Int]]]curr::result的成员

为什么cons不是immutable.this.IndexedSeq[List[List[scala.this.Tuple2[scala.this.Char,scala.this.Int]]]curr::result的成员,scala,functional-programming,Scala,Functional Programming,给定一个元组列表(Char,Int),我想找到列表的子集,例如,如果我的列表是 List(('a', 2), ('b', 2)) 然后我的函数应该返回 List( List(), List(('a', 1)), List(('a', 2)), List(('b', 1)), List(('a', 1), ('b', 1)), List(('a', 2), ('b', 1)), List(('b', 2)), List(('a', 1), ('b', 2)),

给定一个元组列表(Char,Int),我想找到列表的子集,例如,如果我的列表是

List(('a', 2), ('b', 2))
然后我的函数应该返回

List(
  List(),
  List(('a', 1)),
  List(('a', 2)),
  List(('b', 1)),
  List(('a', 1), ('b', 1)),
  List(('a', 2), ('b', 1)),
  List(('b', 2)),
  List(('a', 1), ('b', 2)),
  List(('a', 2), ('b', 2))
)
我写了这个函数

def combinations(occurrences: List[(Char,Int)]): List[List[(Char,Int)]] = {
  val curr = occurrences filter { case (c,o) => o != 0}
  if (curr.isEmpty) List(List())
  else {
    val result = for{
      i <- 0 until occurrences.length
    } yield combinations(occurrences.updated(i,(occurrences(i)._1,occurrences(i)._2 - 1)))
    curr :: result
  }
}
我修改了我的代码如下

def combinations(occurrences: List[(Char,Int)]): List[List[(Char,Int)]]      = {
  val curr = occurrences filter { case (c,o) => o != 0}
  if (curr.isEmpty) List(List())
  else {
    val result = for{
     (c,o) <- curr
    } yield combinations(occurrences.updated(curr.indexOf((c,o)),(c,o-1)))
    curr :: result
  }
}

简而言之,因为
0直到出现。长度
是一个
范围[Int]
结果
不是一个
列表
,而是一个
IndexedSeq
,其中
不可用

for {
  i <- 0 until occurrences.length
} yield f(i)
然后进入

Range(0, occurrences.length + 1).map(i => f(i))
范围上的
映射
继承自
索引eq
因此具有签名(至少在2.13中):

因此,整个结果类型是一个
IndexedSeq[List[List[(Char,Int)]]
。此结果可以通过
toList
转换为
列表

curr :: (result.toList)

简而言之,因为
0直到出现。长度
是一个
范围[Int]
结果
不是一个
列表
,而是一个
IndexedSeq
,其中
不可用

for {
  i <- 0 until occurrences.length
} yield f(i)
然后进入

Range(0, occurrences.length + 1).map(i => f(i))
范围上的
映射
继承自
索引eq
因此具有签名(至少在2.13中):

因此,整个结果类型是一个
IndexedSeq[List[List[(Char,Int)]]
。此结果可以通过
toList
转换为
列表

curr :: (result.toList)

正如我在上面的评论中所提到的,我找到了几种不同的方法来解决类型冲突,但不是以产生所需结果的方式

def combinations(occurrences: List[(Char,Int)]): List[List[(Char,Int)]] = {
  val curr = occurrences filter { case (c,o) => o != 0 }
  if (curr.isEmpty) List(List())
  else {
    val result = for {
      (c, o) <- curr
      res <- combinations(curr.updated(curr.indexOf((c, o)), (c, o-1)))
// using occurrences here ^^^^ tends to StackOverflow
    } yield res
    curr :: result
  }
}

这里有一种不同的方法(实际上似乎有效)


正如我在上面的评论中所提到的,我找到了几种不同的方法来解决类型冲突,但不是以产生所需结果的方式

def combinations(occurrences: List[(Char,Int)]): List[List[(Char,Int)]] = {
  val curr = occurrences filter { case (c,o) => o != 0 }
  if (curr.isEmpty) List(List())
  else {
    val result = for {
      (c, o) <- curr
      res <- combinations(curr.updated(curr.indexOf((c, o)), (c, o-1)))
// using occurrences here ^^^^ tends to StackOverflow
    } yield res
    curr :: result
  }
}

这里有一种不同的方法(实际上似乎有效)


有许多不同的方法可以修复您看到的类型错误。好消息是,使用一两个小mod,您的代码将编译并运行。坏消息是,到目前为止,我遇到的所有修复程序在应用时都不会产生您想要的输出。感谢您的帮助,您使用了哪些修复程序来编译和运行它?有许多不同的方法来修复您看到的类型错误。好消息是,使用一两个小mod,您的代码将编译并运行。坏消息是,到目前为止,我遇到的所有修复程序在应用时都不会产生您想要的输出。感谢您的帮助,您使用了哪些修复程序来编译和运行它?非常感谢您的帮助:)非常感谢您的帮助:)
def combinations(occurrences: List[(Char,Int)]): List[List[(Char,Int)]] = {
  val curr = occurrences filter { case (c,o) => o != 0 }
  if (curr.isEmpty)
    List(List()) // or Nil for a different wrong result
  else {
    val result = curr.flatMap { case (c, o) =>
      combinations(curr.updated(curr.indexOf((c, o)), (c, o-1)))
    }
    curr :: result
  }
}
def crossProd[T](in: Seq[T]*): Seq[Seq[T]] =
  if (in.isEmpty) in
  else in.tail.foldLeft(in.head.map(Seq(_))) {
    for {sentence <- _; word <- _} yield sentence :+ word
  }

def combinations(occurrences: List[(Char,Int)]) : List[List[(Char,Int)]] =
  crossProd(occurrences.map{case (c,n) => (0 to n).map(x => (c,x))}:_*)
    .map(_.filter(_._2 > 0).toList)
    .toList
combinations(List(('c',2),('x',3),('#',1)))
//res0: List[List[(Char, Int)]] = 
// List(List()
// , List((#,1))
// , List((x,1))
// , List((x,1), (#,1))
// , List((x,2))
// , List((x,2), (#,1))
// , List((x,3))
// , List((x,3), (#,1))
// , List((c,1))
// , List((c,1), (#,1))
// , List((c,1), (x,1))
// , List((c,1), (x,1), (#,1))
// , List((c,1), (x,2))
// , List((c,1), (x,2), (#,1))
// , List((c,1), (x,3))
// , List((c,1), (x,3), (#,1))
// , List((c,2))
// , List((c,2), (#,1))
// , List((c,2), (x,1))
// , List((c,2), (x,1), (#,1))
// , List((c,2), (x,2))
// , List((c,2), (x,2), (#,1))
// , List((c,2), (x,3))
// , List((c,2), (x,3), (#,1)))