【发布时间】:2016-01-26 18:33:08
【问题描述】:
我的一个练习要求我编写一个递归方法,其中给出了一个列表,它返回相同的列表,其中只有所有其他元素。
例如:List {"a", "b", "c"} 将返回 列出{"a","c"}
我正在用 scala 编写,我知道它已经内置在库中,但我不应该使用这些。我只能使用 if/else、辅助方法和模式。
如何仅使用头尾来解析列表?
到目前为止我有这个:
def removeLetter(list:List[String]):List[String]=list match{
case Nil => Nil
case n::rest=>
if (n == rest){ // I understand that this doesn't quite work.
tail
}
else
head::removeLetter(tail)
}
}
我在寻找逻辑而不是代码。
【问题讨论】: