A procedure gives a process a name.
Build a useful operation from smaller ones and watch names become parameters when the procedure is applied.
How does a procedure definition turn an expression into a reusable method?
- Read a procedure definition as parameters plus a body
- Follow one compound procedure application
- Distinguish a name from the value bound to it
square names a one-parameter procedure. sum-of-squares then combines two applications of square. The definition records a method. Nothing in its body runs until the procedure is applied.
On application, each parameter is bound to an argument value in a new environment. The body is evaluated with those bindings, producing 25 for inputs 3 and 4.
(begin
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(sum-of-squares 3 4))- Output
- —
- Value
- —
- Diagnostic
- —
The first program returns 25 and produces no output.
Find the two square applications. They use different x bindings even though both calls evaluate the same procedure body.
Change the program before you read the hint.
Define sum-of-cubes using a cube procedure, then evaluate it for 2 and 3.
Show one hint
Keep the combining procedure independent from the details of cubing one value.