2017-06-29 75 views
0

我在coursera上做scala课程。我正在经历第6周的任务。我坚持组合功能。scala coursera第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) 
} 

的方法是很长,但它看起来可读我。我得到的声明:组合< - restTuplesCombination(其余,xs)

我不明白什么是打破代码在这里。 for循环中的两个表达式都返回两个集合,我使用yield来创建元素的组合。

请让我知道我在这里做错了什么。

谢谢

回答

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表达多层次这样是首选的风格。


我说“默认”,这里因为有一些情况下,当分析器会自动两行合并成一个单一的表达;例如,如果一行以二元运算符结束,则下一行被假定为二元运算符的右手变元。因此,以下两个花括号封闭的表达式被解析为相同:

{ 
    1 + 
    2 
} 

{ 1 + 2 } 
+0

谢谢道文。我现在明白这个区别。 – kromastorm

2

您有一个语法问题。如果更换周围的圆形支架用大括号分配它编译:

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

谢谢@Steffen,这解决了我的问题。 – kromastorm