scala: make a function tail recursive

sea*_*eas 1 scala tail-recursion

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 I got stuck ...);
    }
  }
  is_in_tailrec(e, as, 1);
}
Run Code Online (Sandbox Code Playgroud)

Could someone please give me an advice how I can make this function tail recursive?

Due*_*ist 5

实际上,这里不需要带累加器的辅助方法。只需检查是否e == x返回false,然后使用列表的其余部分调用该方法,否则返回true:

  def is_in[T](e: T, as: List[T]): Boolean = as match {
    case Nil => false
    case x :: _ if e == x => true
    case _ :: xs => is_in(e, xs)
  }
Run Code Online (Sandbox Code Playgroud)