1.1.6 Scheme procedures
Scheme procedures are executable scheme expressions that return a
value resulting from their execution. They can also manipulate
variables defined outside of the procedure.
Defining procedures
Procedures are defined in Scheme with define
(define (function-name arg1 arg2 ... argn)
scheme-expression-that-gives-a-return-value)
For example, we could define a procedure to calculate the average:
| guile> (define (average x y) (/ (+ x y) 2))
guile> average
#<procedure average (x y)>
|
Once a procedure is defined, it is called by putting the procedure
name and the arguments in a list. For example, we can calculate
the average of 3 and 12:
| guile> (average 3 12)
15/2
|
Predicates
Scheme procedures that return boolean values are often called
predicates. By convention (but not necessity), predicate names
typically end in a question mark:
| guile> (define (less-than-ten? x) (< x 10))
guile> (less-than-ten? 9)
#t
guile> (less-than-ten? 15)
#f
|
Return values
Scheme procedures always return a return value, which is the value
of the last expression executed in the procedure. The return
value can be any valid Scheme value, including a complex data
structure or a procedure.
Sometimes the user would like to have multiple Scheme expressions in
a procedure. There are two ways that multiple expressions can be
combined. The first is the begin
procedure, which allows
multiple expressions to be evaluated, and returns the value of
the last expression.
| guile> (begin (+ 1 2) (- 5 8) (* 2 2))
4
|
The second way to combine multiple expressions is in a let
block.
In a let block, a series of bindings are created, and then a sequence
of expressions that can include those bindings is evaluated. The
return value of the let block is the return value of the last
statement in the let block:
| guile> (let ((x 2) (y 3) (z 4)) (display (+ x y)) (display (- z 4))
… (+ (* x y) (/ z x)))
508
|
Andere talen: deutsch, español, français.
About automatic language selection.