【发布时间】:2019-11-29 19:26:02
【问题描述】:
我在scala中有以下功能:
def is_in[T](e: T, as: List[T]) : Boolean = as match
{
case Nil => false;
case x::xs => e == x || is_in(e, xs);
}
现在我想让这个函数尾递归。我的想法如下:
// tail recursive:
def is_in[T](e: T, as:List[T]) : Boolean =
{
@tailrec
def is_in_tailrec[T](e: T, as:List[T], acc: Boolean) : Boolean =
{
as match
{
case Nil => acc;
case x::xs => is_in_tailrec(... here I got stuck ...);
}
}
is_in_tailrec(e, as, 1);
}
谁能给我一个建议,我怎样才能使这个函数尾递归?
【问题讨论】:
-
已经是尾递归了。
标签: scala tail-recursion