Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.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
scala coursera第6周作业方法编译错误_Scala - Fatal编程技术网

scala coursera第6周作业方法编译错误

scala coursera第6周作业方法编译错误,scala,Scala,我正在coursera上scala课程。我正在完成第六周的作业。我被组合功能卡住了 以下是问题的说明: type Occurrences = List[(Char, Int)] /** * Returns the list of all subsets of the occurrence list. * This includes the occurrence itself, i.e. `List(('k', 1), ('o', 1))` * is a subset of

我正在coursera上scala课程。我正在完成第六周的作业。我被组合功能卡住了

以下是问题的说明:

type Occurrences = List[(Char, Int)]
/**
   * Returns the list of all subsets of the occurrence list.
   *  This includes the occurrence itself, i.e. `List(('k', 1), ('o', 1))`
   *  is a subset of `List(('k', 1), ('o', 1))`.
   *  It also include the empty subset `List()`.
   *
   *  Example: the subsets of the occurrence list `List(('a', 2), ('b', 2))` are:
   *
   *    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))
   *    )
   *
   *  Note that the order of the occurrence list subsets does not matter -- the subsets
   *  in the example above could have been displayed in some other order.
   */

  def combinations(occurrences: Occurrences): List[Occurrences] =???
以下是基于我所能理解的逻辑的解决方案:

def combinations(occurences: Occurrences) : List[Occurrences] = {

  def restTuplesCombination(occ: Occurrences, xs: List[Occurrences]): List[Occurrences] = occ match {
    case Nil=> xs :+ Nil
    case head :: rest => {
      for(
        entry <- headTupleCombination(head)
        combination <- restTuplesCombination(rest, xs) // getting error here
      ) yield if(entry._2 == 0)
        combination
      else
        entry :: combination
      // case close
    }
  }

  def headTupleCombination(tuple: (Char, Int) ):List[( Char, Int)] = {
    if(tuple._2 < 0)
      Nil
    else
      tuple :: headTupleCombination( (tuple._1, tuple._2 -1))
  }

  restTuplesCombination(occurences, Nil)
}
def组合(事件:事件):列表[事件]={
def restTuplesCombination(occ:occurrencess,xs:List[occurrencess]):List[occurrencess]=occ匹配{
案例Nil=>xs:+Nil
案例标题::rest=>{
为了(

条目这里有一个语法问题。如果将for赋值周围的圆括号替换为大括号,它将编译:

type Occurrences = List[(Char, Int)]

def combinations(occurences: Occurrences) : List[Occurrences] = {

  def restTuplesCombination(occ: Occurrences, xs: List[Occurrences]): List[Occurrences] = occ match {
    case Nil=> xs :+ Nil
    case head :: rest =>
      for {
        entry <- headTupleCombination(head)
        combination <- restTuplesCombination(rest, xs)
      } yield
        if(entry._2 == 0)
          combination
        else
          entry :: combination
  }

  def headTupleCombination(tuple: (Char, Int) ): List[( Char, Int)] = {
    if(tuple._2 < 0)
      Nil
    else
      tuple :: headTupleCombination( (tuple._1, tuple._2 -1))
  }

  restTuplesCombination(occurences, Nil)
}
类型引用=列表[(字符,整数)]
def组合(事件:事件):列表[事件]={
def restTuplesCombination(occ:occurrencess,xs:List[occurrencess]):List[occurrencess]=occ匹配{
案例Nil=>xs:+Nil
案例标题::rest=>
为了{

条目这里有一个语法问题。如果将for赋值周围的圆括号替换为大括号,它将编译:

type Occurrences = List[(Char, Int)]

def combinations(occurences: Occurrences) : List[Occurrences] = {

  def restTuplesCombination(occ: Occurrences, xs: List[Occurrences]): List[Occurrences] = occ match {
    case Nil=> xs :+ Nil
    case head :: rest =>
      for {
        entry <- headTupleCombination(head)
        combination <- restTuplesCombination(rest, xs)
      } yield
        if(entry._2 == 0)
          combination
        else
          entry :: combination
  }

  def headTupleCombination(tuple: (Char, Int) ): List[( Char, Int)] = {
    if(tuple._2 < 0)
      Nil
    else
      tuple :: headTupleCombination( (tuple._1, tuple._2 -1))
  }

  restTuplesCombination(occurences, Nil)
}
类型引用=列表[(字符,整数)]
def组合(事件:事件):列表[事件]={
def restTuplesCombination(occ:occurrencess,xs:List[occurrencess]):List[occurrencess]=occ匹配{
案例Nil=>xs:+Nil
案例标题::rest=>
为了{

条目SteffenSchmitz的回答是正确的,但这里有一些关于这个问题的澄清:

圆括号
()
和花括号
{}
在Scala中的解释稍有不同。在圆括号之间指定的任何不由逗号或分号分隔的内容都被视为单个表达式。相反,在花括号之间指定的任何内容默认情况下被视为每行一个表达式。†

换句话说,这部分代码:

  for(
    entry <- headTupleCombination(head)
    combination <- restTuplesCombination(rest, xs)
  )

SteffenSchmitz的回答是正确的,但这里有一些关于这个问题的澄清:

圆括号
()
和花括号
{}
在Scala中的解释稍有不同。在圆括号之间指定的任何不由逗号或分号分隔的内容都被视为单个表达式。相反,在花括号之间指定的任何内容默认情况下被视为每行一个表达式。†

换句话说,这部分代码:

  for(
    entry <- headTupleCombination(head)
    combination <- restTuplesCombination(rest, xs)
  )

谢谢道文。我现在明白这个区别了。谢谢道文。我现在明白这个区别了。
  for {
    entry <- headTupleCombination(head)
    combination <- restTuplesCombination(rest, xs)
  }
{
  1 +
  2
}

{ 1 + 2 }