Evaluation Strategies
In this lesson, we will be going over the evaluation strategy Scala uses to evaluate expressions.
The Substitution Model
Scala uses the substitution model for evaluating expressions. The idea underlying this model is that evaluation simply reduces expressions to a value.
Let’s call the squareSum
function we created in the first lesson and see how the substitution model would evaluate it. To make things interesting we will pass an expression which reduces to a Double
as one of the parameters.
Scala
def square(x: Double) ={x * x}def squareSum(x: Double, y: Double) ={square(x) + square(y)}val total = squareSum(2,4+1) // 4+1 reduces to 5 so this is a valid parameter// Driver Codeprintln(total)
When you run the code above, you should get 29.0 ...