Nyní, když víte, že každý výrok ve Scheme je uzavřen v kulatých závorkách a že jméno funkce či operátor je v závorkách uveden na prvním místě, je třeba se naučit a vytvářet proměnné a funkce. Začneme proměnnými.
Although there are a couple of different methods for declaring variables, the preferred method is to use the let* construct. If you're familiar with other programming languages, this construct is equivalent to defining a list of local variables and a scope in which they're active. As an example, to declare two variables, a and b, initialized to 1 and 2, respectively, you'd write:
(let*
(
(a 1)
(b 2)
)
(+ a b)
)
nebo na jedné řádce:
(let* ( (a 1) (b 2) ) (+ a b) )
|
Poznámka |
|---|---|
|
Používáte-li Script-Fu konzoli, je třeba zadat vše na jednom řádku. V ostatních případech je však dobré používat přehledné odsazování pro zvýšeni čitelnosti kódu. Více si o tom povíme v části věnované bílým znakům. |
Takto jsou deklarovány dvě lokální proměnné, a a b, inicializovány a je vypsán jejich součet.
Jistě jste si všimli, že součet (+ a b) byl zapsán uvnitř závorek výrazu let*, nikoliv za nimi.
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.
Zápis výroku let* je obecně následující:
(let* ( variables )
expressions )
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.
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.
Once you've initialized a variable, you might need to change its value
later on in the script. Use the set! statement to change
the variable's value:
(let* ( (theNum 10) ) (set! theNum (+ theNum theNum)) )
Pokuste se odhadnout, co výše uvedený výrok dělá, a pak ho zadejte do Script-Fu konzole.
Nyní, když umíte pracovat s proměnnými, se podíváme na funkce. K deklaraci funkce slouží následující syntaxe:
(define
(
name
param-list
)
expressions
)
kde jmeno je jméno funkce, seznam-parametru je mezerami oddělený seznam jmen parametrů funkce a vyrazy je řada výrazů, které funkce při svém zavolání vykoná. Například:
(define (AddXY inX inY) (+ inX inY) )
Tato funkce se jmenuje AddXY a má dva parametry, proměnné inX a inY. Funkce oba své parametry sečte.
Pokud jste programovali v jiných imperativních jazycích (např. v C/C++, Javě, Pascalu atd.), možná jste si všimli, že definice funkce neobsahuje ve srovnání s jinými jazyky řadu položek.
First, notice that the parameters don't have any „types“ (that is, we didn't declare them as strings, or integers, etc.). Scheme is a type-less language. This is handy and allows for quicker script writing.
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)