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)