Node:Definition, Previous:Values and Variables, Up:About Data

[Показать/скрыть оригинал] [Показать/скрыть перевод] [Переключить перевод и оригинал]


14.1.3 Defining and Setting Variables

13.1.3 Определение и установка значений перменных

To define a new variable, you use Scheme's define syntax like this:

Для определения новой перменной в Scheme используется следующий синтаксис оператора define:

(define variable-name value)

This makes a new variable called variable-name and stores value in it as the variable's initial value. For example:

Создаётся новая перменная, называющаяся variable-name и сохраняющая value в качестве начального значения. Например:

;; Make a variable `x' with initial numeric value 1.
(define x 1)

;; Make a variable `organization' with an initial string value.
(define organization "Free Software Foundation")

(In Scheme, a semicolon marks the beginning of a comment that continues until the end of the line. So the lines beginning ;; are comments.)

(В Scheme точка с запятой обозначает начало комментария, длящегося до конца строки. Так что ;; в начале - это комментарий)

Changing the value of an already existing variable is very similar, except that define is replaced by the Scheme syntax set!, like this:

Изменение уже существующей переменной очень похоже, за исключением того, что define заменяется другим оператором set!:

(set! variable-name new-value)

Remember that variables do not have fixed types, so new-value may have a completely different type from whatever was previously stored in the location named by variable-name. Both of the following examples are therefore correct.

Помните, что перменные не имеют фиксированного типа, так что new-value может быть совершенно другого типа по сравнению с предыдущим значнием, хранившемся в variable-name. Оба нижеследюущие примера абсолютно правильны:

;; Change the value of `x' to 5.
(set! x 5)

;; Change the value of `organization' to the FSF's street number.
(set! organization 545)

In these examples, value and new-value are literal numeric or string values. In general, however, value and new-value can be any Scheme expression. Even though we have not yet covered the forms that Scheme expressions can take (see About Expressions), you can probably guess what the following set! example does...

В вышеприведённых примерах value и new-value это символьные, числовые и строковые значения. Однако, в общем случае, value and new-value могут быть произвольным выражением, поддерживаемым Scheme. Не смотря на то, что мы ещё не рассматривали выражения в Scheme, вероятнее всего вы сможете понять, что делают эти примеры:

(set! x (+ x 1))

(Note: this is not a complete description of define and set!, because we need to introduce some other aspects of Scheme before the missing pieces can be filled in. If, however, you are already familiar with the structure of Scheme, you may like to read about those missing pieces immediately by jumping ahead to the following references.

(Примечание: В связи с тем, что мы ещё не рассмотрели многие аспекты Scheme, мы не приводим тут полное описание синтаксиса define и set!. Но если вы уже знакомы с Scheme, то может иметь смысл пропустить введение и начать читать полное описание:


> > далее > >