I am writing a function that takes a list of occurrence characters ( List[(Char, Int)]) in a string and creates all subsets of this event list.
So, given
List(('a', 2), ('b', 2))
He will produce
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))
)
I implemented it as follows:
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)
And I get this error:
type mismatch;
found : List[List[Product with Serializable]]
required: List[Occurrences]
(which expands to) List[List[(Char, Int)]]
Please help me understand why this is happening and how to fix it?
I tried to rewrite it as flatMap ... using Intellij's "Explain Scala Code", etc.