Now we will learn about procedure definitions, a much more powerful abstraction technique by which a compound operation can be given a name and then referred to as a unit. The following is a typical procedure definition.
Procedure: (param1: type1, param2: type2, param3: type3) → type4
(define (procedure param1 param2 param3) (body))
Square: (x: number) → number
; the square a number, multiply it by itself
(define (square x) (* x x))
; some example uses
(square 21) ; => 441
(square (+ 2 5)) ; => 49
SumSquares: (x: number, y: number) → number
; use square to define sum of squares
(define (sum-squares x y)
(+ (square x) (square y)))
(define (distance x y)
(sqrt (sum-squares x y)))
We can assume that the mechanism for applying primitive procedures to arguments is built into the interpreter. For compound procedures, the application process is as follows:
To apply a compound procedure to arguments, evaluate the body of the procedure with each formal parameter replaced by the corresponding argument.
How it really works…
Typical interpreters do not evaluate procedure applications by manipulating the text of a procedure to substitute values for the formal parameters. In practice, the “substitution” is accomplished by using a local environment for the formal parameters.
Scheme Interpreter uses “evaluate the arguments and then apply” method, which is called the applicative-order evaluation.
But there is an alternative: “fully expand and then reduce” evaluation method, or also known as normal-order evaluation.
It can be shown that, for procedure applications that can be modeled using substitution and that yield legitimate values, normal-order and applicative-order evaluation produce the same value. Nonetheless, Scheme uses applicative-order, mainly because normal-order evaluation becomes much more complicated to deal with when we leave the realm of procedures that can be modeled by substitution.
A procedure is a pattern for the local evolution of a computational process. It specifies how each stage of the process is built upon the previous stage. We would like to be able to make statements about the overall, or global, behavior of a process whose local evolution has been specified by a procedure. It is is very difficult to do in general, but we can at least try to describe some typical patterns of process evolution.