스칼라에는 다음과 같은 기능이 있습니다.
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);
}
누군가이 함수 꼬리를 재귀 적으로 만들 수있는 방법에 대한 조언을 해주시겠습니까?
내 제안은
{
case Nil => acc
case _ if acc => acc
case x :: xs => is_in_tailrec(e, xs, x == e)
}
또는 심지어
{
case x :: xs if !acc => is_in_tailrec(e, xs, x == e)
case _ => acc
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다