![]() |
Not |
---|---|
This section as adapted from a tutorial written for the Gimp 1 User Manual by Mike Terry. |
Every statement in Scheme is surrounded by parentheses ().
The second thing you need to know is that:
The function name/operator is always the first item in the parentheses, and the rest of the items are parameters to the function.
However, not everything enclosed in parentheses is a function -- they can also be items in a list -- but we'll get to that later. This notation is referred to as prefix notation, because the function prefixes everything else. If you're familiar with postfix notation, or own a calculator that uses Reverse Polish Notation (such as most HP calculators), you should have no problem adapting to formulating expressions in Scheme.
The third thing to understand is that:
Mathematical operators are also considered functions, and thus are listed first when writing mathematical expressions.
This follows logically from the prefix notation that we just mentioned.
Typing this in and hitting Return yields the expected answer of 8 in the center window.
Now, what if we wanted to add more than one number? The "+" function can take two or more arguments, so this is not a problem:
This also yields the expected answer of 14.
So far, so good -- we type in a Scheme statement and it's executed immediately in the Script-Fu Console window. Now for a word of caution....
Knowing that the + operator can take a list of numbers to add, you might be tempted to convert the above to the following:
However, this is incorrect -- remember, every statement in Scheme starts and ends with parens, so the Scheme interpreter will think that you're trying to call a function named "5" in the second group of parens, rather than summing those numbers before adding them to 3.
The correct way to write the above statement would be:
These are all accepted by C/C++, Perl and Java compilers. However, the same is not true for Scheme. You must have a space after a mathematical operator (or any other function name or operator) in Scheme for it to be correctly interpreted by the Scheme interpreter.
Practice a bit with simple mathematical equations in the Script-Fu Console until you're totally comfortable with these initial concepts.