Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Last active July 7, 2019 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j5ik2o/c7dcf6893b2b6bf9239d0ca53512f3bc to your computer and use it in GitHub Desktop.
Save j5ik2o/c7dcf6893b2b6bf9239d0ca53512f3bc to your computer and use it in GitHub Desktop.

入力されたリストの要素を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