Skip to content

2016 08 09 cbk

baekkyu cho edited this page Aug 8, 2016 · 16 revisions

2장 스칼라로 함수형 프로그래밍 시작하기

<요약>
- 꼬리재귀함수를 이용하여 loop를 작성하는 방법
- 고차함수 소개
- 다형적 고차함수의 예

2.1 스칼라 언어의 소개

  • 기본적인 예제 하나
// 주석
/* 주석 */
/** 주석 */
object MyModule {    //object 키워드는 singleton 형식을 만든다. (java의 static과 비슷)
  def abs(n: Int): Int =
    if (n < 0) -n
    else n

  private def formatAbs(x: Int) = {
    val msg = "The absolute value of %d is %d"
    msg.format(x, abs(x))
  }

  def main(args: Array[String]): Unit =    //Unit은 java, c의 void와 같은 목적
    println(formatAbs(-42))
}

Clone this wiki locally