hello world+factorial 함수 만들기(스칼라)

짜리몽땅개발자·2021년 10월 8일
0

함수형 언어 스칼라에 대해서 알아보자.

import java.math.BigInteger

object MainClass {
  def main(args: Array[String]): Unit = {
    println("hello scala world!")

    def factorial(x: BigInt): BigInt =
      if x == 0 then 1 else x * factorial(x - 1)

    // 결과는 같다.
    println(factorial(30)) // 스칼라
    println(factorialj(BigInteger.valueOf(30))) // 자바함수 사용, 자바 내장함수 사용해야 한다.
  }
}

def factorialj(x: BigInteger): BigInteger =
  if x == BigInteger.ZERO then BigInteger.ONE else x.multiply(factorialj(x.subtract(BigInteger.ONE)))
profile
시간은 돈과 바꿀 수 있다.

0개의 댓글