| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
3.2.4 Lambda Alternatives
Since it is so common in Scheme programs to want to create a procedure
and then store it in a variable, there is an alternative form of the
define syntax that allows you to do just that.
A define expression of the form
(define (name [arg1 [arg2 …]]) expression …)
is exactly equivalent to the longer form
(define name
(lambda ([arg1 [arg2 …]])
expression …))
So, for example, the definition of make-combined-string in the
previous subsection could equally be written:
(define (make-combined-string name address) (string-append "Name=" name ":Address=" address))
This kind of procedure definition creates a procedure that requires
exactly the expected number of arguments. There are two further forms
of the lambda expression, which create a procedure that can
accept a variable number of arguments:
(lambda (arg1 … . args) expression …) (lambda args expression …)
The corresponding forms of the alternative define syntax are:
(define (name arg1 … . args) expression …) (define (name . args) expression …)
For details on how these forms work, see See section Lambda: Basic Procedure Creation.
(It could be argued that the alternative define forms are rather
confusing, especially for newcomers to the Scheme language, as they hide
both the role of lambda and the fact that procedures are values
that are stored in variables in the some way as any other kind of value.
On the other hand, they are very convenient, and they are also a good
example of another of Scheme’s powerful features: the ability to specify
arbitrary syntactic transformations at run time, which can be applied to
subsequently read input.)
| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on February 3, 2012 using texi2html 5.0.
