Here are my notes for the week 1’s lecture of Functional Programming Principles in Scala on Coursera.
Evaluation of Programming
Every non-trivial programming language provides:
- primitive expressions representing the simplest elements
- ways to combine expressions
- ways to abstract expressions, which introduces a name for an expression by which it can be referred to
A non-primitive expression is evaluated as follows:
- Take the leftmost operator
- Evaluate its operands (left before right)
- Apply the operator to the operands
A name is evaluated by replacing it with the right hand side of its definition.
(2 * pi) * radius
(2 * 3.14159) * radius
6.28318 * radius
6.28318 * 10
Definition can have parameters:
def square(x: Double) = x * x
Parameter and Return Types:
def power(x: Double, y: Int): Double = ...
Evaluation of Function Applications
- Evaluate all function arguments, from left to right.
- Replace the function application by the function’s right-hand side, and, at the same time,
- Replace the formal parameters of the function by the actual arguments.
square(3) + square(4)
3 * 3 + square(4)
9 + square(4)
9 + 4 * 4
9 + 16
The substitution model
The scheme of expression evaluation is called the substitution model.
The idea is that all evaluation dose is reduce an expression to a value.
It can be applied to all expressions, as long as they have no side effects.
The substitution model is formalized in the λ-calculus, which gives a
foundation for functional programming.