Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Last active July 7, 2019 06:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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