【发布时间】:2016-05-24 08:22:24
【问题描述】:
我是 Scala 的新手。我对 foo 循环中的顺序有疑问。
type Occurrences = List[(Char, Int)]
lazy val dictionaryByOccurrences: Map[Occurrences, List[Word]] = dictionary.groupBy(x => wordOccurrences(x))
def wordAnagrams(word: Word): List[Word] = dictionaryByOccurrences.getOrElse(wordOccurrences(word), List())
def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match {
case List() => List(List())
case head::tail => {
for (o <- combinations(tail); x <- 1 to head._2)
yield (head._1, x) :: o
}
如果我在for循环中改变顺序,那就错了
def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match {
case List() => List(List())
case head::tail => {
for (x <- 1 to head._2; o <- combinations(tail))
yield (head._1, x) :: o
}
找不到原因
【问题讨论】:
标签: scala