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)
- the reduced expression consists of pure functions, and
- both evaluations terminate. νμ μ νλΌλ―Έν°(parameters)λ‘ μΈμ(arguments)λ₯Ό λκΈΈ λ, CBVλ κ°μΌλ‘ μ λ¬λκ³ CBNμ μ€μ νλΌλ―Έν°κ° μ¬μ©λλ λΆλΆμμ evaluation λλ μ°¨μ΄κ° μλ€.
Evaluation of Function Applications
- μ’μμ μ°λ‘ μΈμμ κ°μ evaluate
- νμ μ μ°μΈ‘(μΈμκ°λ€)μ λμμ νμ (function application)μΌλ‘ μΉν
- ν¨μμ νμνλΌλ―Έν°λ₯Ό μ€μ μΈμ(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
λ΄λΆμ μΌλ‘λ§ μ¬μ©λλ ν¨μλ₯Ό λ€λ₯Έ ν¨μ λ΄λΆμ ν¬ν¨μν¬ μ μμ.
- [local functionμ μ¬μ©νμ§ μμ SqureRoot μ½λ] (https://github.com/unlimitedfocus/progfun-review/blob/master/week1/src/main/scala/SquareRoot.scala)
- [local functionμ νμ©ν SqureRoot μ½λ] (https://github.com/unlimitedfocus/progfun-review/blob/master/week1/src/main/scala/SquareRoot2.scala)