Fppis week 1 - codeport/scala GitHub Wiki

Week 1

Programming Paradigms

Why Scala

스칼라의 장점

  • jvm 기반, 자바와의 호환성
  • concise - syntactic sugar, case class, generic 등의 간결성
  • high level - 강의의 예시가 적절하지는 않으나, Collection 처리를 하는 부분에서 iteration 하는 부분과 logic을 분리한 부분을 뜻하는 것으로 판단.
  • statically typed - 똑똑한 타입 추론, boiler plate 한 코드 작성을 줄일 수 있음

The Predicate

A function literal can be called a predicate if its reulst type is Boolean boolean 타입의 조건절

Substution Model

람다계산식 (Lambda Calculus) 와 관계됨. 문제를 추상적 거시적으로 볼 수 있도록 한다.

def f(x1,...,xn) = B; ... f(v1,..., vn)
then
def f(x1,...,xn) = B; ... [v1/x1,...,vn/xn]B

def vs val

loop 부분 참고

  • def - 함수를 호출 할 때마다. evaluation 과정을 거침
  • val - 값이 할당 되는 순간 고정

call by value(CBV) vs call by name(CBN)

  1. the reduced expression consists of pure functions, and
  2. both evaluations terminate. 펑션의 파라미터(parameters)로 인자(arguments)를 넘길 때, CBV는 값으로 전달되고 CBN은 실제 파라미터가 사용되는 부분에서 evaluation 되는 차이가 있다.

Evaluation of Function Applications

  1. 좌에서 우로 인자의 값을 evaluate
  2. 펑션의 우측(인자값들)을 동시에 펑션(function application)으로 치환
  3. 함수의 형식파라미터를 실제 인자(arguement)로 치환

block lexical scope

자바스크립트를 제외한 언어는 모두 비슷한 것 같다. 스칼라도 마찬가지

  val x = 0
  
  def f(y: Int) = y + 1
  
  val result = {
    val x = f(3); x * x
  } + x

tail recursion

함수의 바디를 재활용하여 힙에 지속적으로 스택이 쌓이지 않도록 한다.

  • 몇 가지 제약 때문에 accumulator 역할을 하는 파라미터가 필요할수도 있다.
  • 반드시 리턴타입을 명시해야함
  • @tailrec 어노테이션으로 확인 가능
  • GCD
  • tailrec을 사용하는 factorial

sbt

프로젝트 구조를 생성하는 스크립트

local function

내부적으로만 사용되는 함수를 다른 함수 내부에 포함시킬 수 있음.

  1. [local function을 사용하지 않은 SqureRoot 코드] (https://github.com/unlimitedfocus/progfun-review/blob/master/week1/src/main/scala/SquareRoot.scala)
  2. [local function을 활용한 SqureRoot 코드] (https://github.com/unlimitedfocus/progfun-review/blob/master/week1/src/main/scala/SquareRoot2.scala)