小编sea*_*eas的帖子

scala: make a function tail recursive

I have the following function in 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);
}
Run Code Online (Sandbox Code Playgroud)

Now I want to make this function tail recursive. My idea is the following:

// 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 …
Run Code Online (Sandbox Code Playgroud)

scala tail-recursion

1
推荐指数
1
解决办法
90
查看次数

标签 统计

scala ×1

tail-recursion ×1