スカラー尾の再帰に関する質問があります。私はリストを取り、偶数の新しいリストを作成する単純なテール再帰コードを書いた。しかし、スカラーが要素をリストに追加できないため、リストは降順でソートされます。以下は、私は次の質問Scalaでのテール再帰
どのように私は、このメソッド内でリストを逆に文を追加することができますを持っているコード
def listCreator(lists: List[Int]): List[Int] = { @tailrec def evenListCreator(lists: List[Int], accum: List[Int]): List[Int] = { lists match { case Nil => accum case x :: Nil if (isEven (x) == true) => x :: accum case x :: Nil if (isEven (x) == false) => accum case x :: tail if (isEven (x) == true) => evenListCreator(tail, x :: accum) case x :: tail if (isEven (x) == false) => evenListCreator(tail, accum) } } evenListCreator(lists, List()) }
はありますか?
この行
evenListCreator(lists, List())
は、メソッド呼び出しの直後にあり、末尾再帰には必須ですか?
'Vector'は不変であるが、合理的、効率的なアペンドを持って、あなたの代わりに' List' –