问题描述
任务是使用尾递归和匹配表达式删除列表中所有出现的元素。
remove(List(2,1,4,3,2),1),should return : List(2,2)
这是一个伪代码,仅适用于第一次出现。有人可以指导我如何做,以便消除所有出现的情况吗? 伪代码
import scala.annotation.tailrec
object Test extends App{
def remove[A](l: List[A],el: A): List[A] = {
@tailrec
def helper(l: List[A],acc: List[A] = List[A]()): List[A] = {
l match {
case head :: tail if (head != el) => helper(tail,head::acc)
case head :: tail if (head == el) => (acc.reverse ::: tail)
case _ => acc
}
}
helper(l)
}
print(remove(List(2,1))
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)