Node:Альтернатива Lambda,
Previous:Создание процедуры,
Up:О процедурах
[Показать/скрыть оригинал] [Показать/скрыть перевод] [Переключить перевод и оригинал]
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.
Так как в программах на Scheme часто приходится выполнять последовательность
действий из создания процедуры и сохранения её в переменной, существует альтертнативная форма
синтаксиса define, позволяющая сделать оба действия за раз.
A define expression of the form
Форма выражения define:
(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:
Так что, для примера, определение make-combined-string (упомянутое
в предыдущем параграфе) может быть записано как:
(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-выражения,
которые создают процедуру, принимающую произвольное количество аргументов:
(lambda (arg1 ... . args) expression ...) (lambda args expression ...)
The corresponding forms of the alternative define syntax are:
Соответствующая форма альтертнативого синтаксиса define:
(define (name arg1 ... . args) expression ...) (define (name . args) expression ...)
For details on how these forms work, see See Lambda.
Подробнее о том, как эти формы работают, см: Lambda •.
(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.)
(Альтернативная форма define - предмет споров, поскольку
вызывает путаницу, особенно для новичков в Scheme. Она прячет и роль lambda и факт,
что процедуры - это значения, которые сохраняются в переменных таким же образом, как и все
остальные значения. С другой стороны, она очень удобна и является хорошим примером другой
важной возможности Scheme: возможности производить произвольные синтаксические преобразования
во время исполнения (эта особенность может применяться при последовательном чтении входных данных)).
> > далее > >