scala/functional programming
1.2 프로그래밍의 요소
wefree
2024. 4. 4. 19:47
치환 모델 (substitution model)
- substitution model: evaluation 을 통해 expression 을 value 로 reduce 하는 과정
- 그러면 모든 expression 은 value 로 reduce 될 수 있을까? 다음 예를 보면 아니라는 것을 알 수 있다.
def loop: Int = loop
loop
Call by value vs Call by name
def test(x:Int, y:Int) = x * x
위의 코드에서 call by name 과 call by value 중 어느것이 더 유리할까? 연산 횟수를 비교해 보자
code | #steps of call by value | #steps of call by value |
test(2,3) | 1번 | 1번 |
test(3+4, 8) | 2번 | 3번 |
test(7, 2*4) | 2번 | 1번 |
test(3+4, 2*4) | 3번 | 3번 |
- test(3+4, 8) 가 call by value 에서 연산 횟수가 2번인 이유?
- x = 3+4
- x*x = 7*7
- test(3+4, 8) 가 call by name 에서 연산 횟수가 3번인 이유?
x*x = (3+4) * (3+4) 연산을 해야 함- 3+4 = 7
- 3+4 = 7
- 7*7