3.6.1 Making procedures
Scheme provides a general mechanism for making a procedure:
|Parameters||::$\Rightarrow$||$\epsilon$ | Name Parameters|
Evaluating a ProcedureExpression produces a procedure that takes as
inputs the Parameters following the
form means “make a procedure”. The body of the resulting procedure is
the Expression, which is not evaluated until the procedure is applied.
A ProcedureExpression can replace an Expression. This means anywhere an Expression is used we can create a new procedure. This is very powerful since it means we can use procedures as inputs to other procedures and create procedures that return new procedures as their output!
Here are some example procedures:
(lambda (x) (* x x))
Procedure that takes one input, and produces the square of the input value as its output.
(lambda (a b) (+ a b))
Procedure that takes two inputs, and produces the sum of the input values as its output.
(lambda () 0)
Procedure that takes no inputs, and produces 0 as its output. The result of applying this procedure to any argument is always 0.
(lambda (a) (lambda (b) (+ a b)))
Procedure that takes one input (|a|), and produces as its output a procedure that takes one input and produces the sum of |a| and that input as its output. This is an example of a higher-order procedure. Higher-order procedures produce procedures as their output or take procedures as their arguments. This can be confusing, but is also very powerful.