3.2. 変数と関数

さて今、 どんな Scheme 構文も丸括弧でくくられること、 関数名や演算子はその先頭に置かれることが觧りましたが、 つぎは変数の作り方と使い方や、 関数の作り方と使い方を知っておかなければなりません。 まずは変数から始めましょうか。

3.2.1. 変数の定義

変数の定義のしかたには 2 通りの方法があるのですが、 let* を使う方が好まれています。 他のプログラミング言語に馴染まれた方には、 この定義文が局所 (ローカル) 変数の定義と有効なスコープを定めるコードと同じだと申しあげておきます。 具体的にはたとえば a と b の 2 変数を定義してそれぞれ 1 と 2 で初期化[し、 合算]するのなら、

        (let*
           (
              (a 1)
              (b 2)
           )
           (+ a b)
        )
      

のように書きます。 これはつぎのように 1 行でも書けます。

(let* ( (a 1) (b 2) ) (+ a b) )
[注記] 注記

コンソールウィンドウでは 1 行にまとめて記入しなければなりません。 でも普通はスクリプトが見易くなるように字下げを適宜行ないたいですよね。 この節で空白文字についてもう少し掘り下げて説明します。

このコードは a と b の 2 つの変数を定義して初期化し、 両局所変数の合算を表示して終わります。

3.2.2. 局所変数とは何でしょう

計算式 (+ a b)let* 式のあとではなくその内部に置いたことに注目してください。

This is because the let* statement defines an area in your script in which the declared variables are usable; if you type the (+ a b) statement after the (let* …) statement, you'll get an error, because the declared variables are only valid within the context of the let* statement; they are what programmers call local variables.

3.2.3. let* 構文の一般規則

let* 構文の一般的な形式は、

        (let* (変数名 )
           )
      

where variables are declared within parens, e.g., (a 2), and expressions are any valid Scheme expressions. Remember that the variables declared here are only valid within the let* statement — they're local variables.

3.2.4. 空白文字

Previously, we mentioned the fact that you'll probably want to use indentation to help clarify and organize your scripts. This is a good policy to adopt, and is not a problem in Scheme — white space is ignored by the Scheme interpreter, and can thus be liberally applied to help clarify and organize the code within a script. However, if you're working in Script-Fu's Console window, you'll have to enter an entire expression on one line; that is, everything between the opening and closing parens of an expression must come on one line in the Script-Fu Console window.

3.2.5. 新しい値を変数に代入

一度変数を初期化して使用したあとで、 スクリプト内で別の値を入れて使う必要が生じるかもしれません。 そんなときは set! の出番です。 変数の値を変更する構文はつぎのとおりです。

        (let* ( (いろは 10) ) (set! いろは (+ いろは いろは)) )
      

この構文がどんなことをしているかあててみてください。 そうしたら Script-Fu コンソールに実際に書き込んで試してみましょう。

3.2.6. 関数

さて変数のコツを掴んだところでこんどは関数の扱い方に取り組みましょう。 関数はつぎの構文規則で定義します。

        (define
           (
              関数名
              パラメーターの一群
           )
           
        )
      

ここで 関数名 には関数につけられた名前を、 パラメーターの一群 にはひとつもしくは複数のパラメーターの名前もしくは値を空白で区切って並べます。 には関数が呼ばれたときに実行する一連の式を書きます。 たとえば、

(define (AddXY inX inY) (+ inX inY) )

の関数定義では AddXY が関数名であり、 inXinY が変数名です。 この関数は 2 つのパラメーターを受けとり両者の和を返します。

C/C++ や Java や Pascal のような他の命令型言語のプログラミングを経験された方はそういった言語と比べて見るとこの関数定義に 2 つのことがらが抜け落ちていることに気付かれたことでしょう。

  • ひとつめはどのパラメーターにもがないことです。 つまり文字列型とか整数型といった宣言が見あたりません。 Scheme 言語は型なし言語なのです。 この特徴があるので手軽に素早くスクリプトが書けます。

  • Second, notice that we don't need to worry about how to return the result of our function — the last statement is the value returned when calling this function. Type the function into the console, then try something like:

    (AddXY (AddXY 5 6) 4)