I am trying to implement a method recursively but I am confused because at some point the compiler think it returns List[List[Any]] instead of List[List[Char]]. This is my function:
def anag(wrd: List[Char]): List[List[Char]] = if(wrd.isEmpty) List(wrd)
else wrd.map(l => l :: anag(wrd.tail)) //found: List[List[Any]]
def anag(wrd: List[Char]): List[List[Char]] = if(wrd.isEmpty) List(wrd)
else wrd.map(l => l :: wrd.tail) //OK
What am I missing?
function anag returns List[List[Char]] you are combining it with a char:
The second example is ok since wrd.tail is of type List[Char]
edit:
how to solve that? since this is probably for an assignment on coursera i will leave that to you. You just have to iterate over the whole word AND over all the partial solutions returned by recursive call to anag (look at for comprehensions)
you can also find all permutations of a list with a method permutations from scala library: