scala coursera第6周赋值方法编译错误

时间:2017-06-29 18:00:05

标签: scala

我在课程上做scala课程。我将完成第6周的任务。我遇到了组合功能。

以下是问题的描述:

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)
}

这个方法很长,但对我来说看起来很可读。我正在声明:组合&lt; - restTuplesCombination(rest,xs)

我无法理解这里的代码是什么。 for循环中的表达式都返回两个集合,我使用yield来创建元素组合。

请告诉我这里我做错了什么。

由于

2 个答案:

答案 0 :(得分:3)

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

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

换句话说,这部分代码:

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

的解析方式与您编写的内容相同:

  for(entry <- headTupleCombination(head) combination <- restTuplesCombination(rest, xs)) // all on one line

您可以通过添加分号来解决问题,分号明确告诉解析器您正在编写两个单独的表达式:

  for(
    entry <- headTupleCombination(head); // added semicolon here
    combination <- restTuplesCombination(rest, xs)
  )

或者,正如SteffenSchmitz所建议的那样,您可以将圆括号切换为花括号,这会告诉解析器每行需要一个表达式:

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

对于具有多个级别的for表达式,使用花括号是首选样式。

我说&#34;默认情况下#34;这里是因为在某些情况下,解析器会自动将两行组合成一个表达式;例如,如果一行以二元运算符结束,则假定下一行是二元运算符的右手参数。因此,以下两个大括号括起的表达式被解析相同:

{
  1 +
  2
}

{ 1 + 2 }

答案 1 :(得分:2)

那里有语法问题。如果用花括号替换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)
}