Я пишу функцию, которая будет принимать список вхождений символов (List[(Char, Int)]
) в строку и создавать все подмножества этого списка вхождений.
Итак, учитывая
List(('a', 2), ('b', 2))
Это произведет
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))
)
Я реализовал это так:
type Occurrences = List[(Char, Int)]
def combinations(occurrences: Occurrences): List[Occurrences] =
if (occurrences.isEmpty) List(List())
else for {
(c, n) <- occurrences
i <- n to 1 by -1
} yield (c, i) :: combinations(occurrences.tail)
И я получаю такую ошибку:
type mismatch;
found : List[List[Product with Serializable]]
required: List[Occurrences]
(which expands to) List[List[(Char, Int)]]
Пожалуйста, помогите мне понять, почему это происходит и как это исправить?
Я пробовал переписать его как flatMap ..., используя Intellij "Explain Scala code" и т. Д.