2017-06-29 16 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