Functional Programming (Scala)
8 flashcards · Programming Languages · by Athena Papkouf
non-primitive expression evaluation
1. Take the leftmost operator 2. Evaluate its operands (left before right) 3. Apply the operator to the operands
evaluation of parameterized functions
1. Evaluate all function arguments, from left to right 2. Replace the function application by the function’s right-hand side, and, at the same time 3. Replace the formal parameters of the function by the actual arguments.
Does every expression reduce to a value (in a finite number of steps)?
No. Here is a counter-example def loop: Int = loop loop
call-by-name evaluation
apply the function to unreduced arguments
call-by-value
reduce function arguments to values before rewriting the function application
If CBV evaluation of an expression e terminates, then CBN evaluation of e terminates, too. ▶ The other direction is not true
Scala’s evaluation strategy
Scala normally uses call-by-value. But if the type of a function parameter starts with => it uses call-by-name.
difference between val and def
def form is “by-name”, val form is “by-value”