Scala类型问题:Char到任意

Scala类型问题:Char到任意,scala,Scala,我试图递归地实现一个方法,但我感到困惑,因为在某些时候编译器认为它返回的是List[List[Any]],而不是List[List[Char]]。这是我的职责: 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[Any]]
,而不是
List[List[Char]]
。这是我的职责:

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  

我缺少什么?

函数anag返回List[List[Char]]您正在将其与Char组合:

l :: anag(wrd.tail) //prepending a char to a list of List[Char]
第二个示例没有问题,因为wrd.tail的类型是List[Char]

编辑: 如何解决这个问题?因为这可能是coursera上的作业,所以我将把它留给你。您只需迭代整个单词和递归调用anag返回的所有部分解(查看理解)

您还可以使用scala库中的方法排列查找列表的所有排列:

wrd.permutations.toList

你知道如何把它转换成我想要的吗?你必须弄清楚这段代码要做什么?准确地找到wrd的所有排列。这就是我想要做的:)我成功地编写了一个排列列表的函数,而没有使用已经存在的方法。谢谢:)