Skip to content

Instantly share code, notes, and snippets.

@j5ik2o j5ik2o/末尾再帰.md
Last active Jul 7, 2019

Embed
What would you like to do?

入力されたリストの要素を2倍し合計する関数

def loop(in: List[Int], acc: Int): Int =
  in match {
    case Nil => acc // 終了条件
    case head :: tail =>
      val result = head * 2 // headのタスク
      loop(tail, acc + result) // 残りのリストと計算途中を渡して処理を継続
  }
  
val result = loop((1 to 10).toList, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.