There are a number of Scheme concepts that are important to understand when creating your own Fluent interface. This section discusses those concepts that you are most likely to see in examples throughout this guide and use as you build your own interface.
(define isInt 1) (define whoKnows)
The define statement is used
to create a new variable in Scheme.
Variables in Scheme do not have types, but rather
return a value. The return values themselves have types, but using
the variable name itself is like calling a function that can return
a value of any type. For example, the variable isInt in the example above returns the integer 1.
Scheme variables do not have to be initialized with
a value when they are created. In the example above the variable whoKnows is empty and can be assigned to a value of
any type.
(define someVariable) (set! someVariable 1) (set! someVariable 2.5)
The set! statement is used
to assign a value to a variable that has already been created. If
the variable has not been created via a define statement you will receive an unbound variable error.
Since there are no variable types in Scheme, a variable
can be overwritten with multiple types of data. In the example above,
the variable someVariable is initially empty.
After the first set! statement, someVariable will return the integer value of 1. After the second set! statement
however, someVariable will return the real value of 2.5 instead.
This flexibility in variable use has its advantages, as it is easy to create a variable and begin working with it immediately. It can also be a problem if you aren’t diligent in keeping track of each variable’s return type.
(let ((x 5))
(let ((x 2)
(y x))
(+ y x)))
let statements define the
scope of variable bindings that occur within them. They are similar
to { } in C and C++.
The let statement is broken
up into two parts. The first part begins with the parenthesis immediately
following the word let. Within this set of
parenthesis are the variable bindings that you want to define for
the rest of the let statement.
For the outer let in the
example above, the first part consists of the variable binding (x 5). For the inner let of
the example above the first part consists of the variable bindings (x 2) and (y x).
The second part of the let statement is known as the body. Once the variable bindings are complete,
this is where the operations using these variables occurs.
For the outer let in the
example above, the body consists of everything that follows the first
part, including the inner let, until the
final parenthesis. For the inner let in the
example above, the body includes the statement (+ y x).
When lets are nested, as
in the example above, and both the outer and inner let bind a value to the same variable, the value bound by the outer let is the one used until the first part of the inner let is closed.
Therefore, the (+ y x) statement
in the example above will use the value 2 for x as opposed to 5. The value for y in this statement, however,
will still be 5 because when y was assigned the value of x, it didn’t see that x had been changed
to 2 because the first part of the inner
let wasn’t closed yet. This makes the result of the addition 7.
(define doubleProcedure (lambda (x) (+ x x))) (doubleProcedure 1)
The lambda keyword is used
to create a new Scheme procedure.
When using the lambda keyword
to make a new procedure, you must first provide a list of the variables
that will be used in the procedure. Once the variables are declared,
you can then specify the functionality of the procedure.
This process is seen in the example above. Once the
keyword lambda is used to make doubleProcedure a procedure, the variable x is declared. Since x is the
only variable being used in this procedure, we can now move on to
the functionality, which consists of the (+ x x) portion of the line.
Now that the procedure has been created, we can call
it using any number for x, as seen in the
second line of the example above, which will return 2.
(define ifExample (lambda (x) (if (>= x 0) (+ x 1) (- x 1)))) (ifExample 1) (ifExample -1)
The Scheme if statement has
the same basic logic as if statements in
most other programming languages, though the syntax is slightly different.
In Scheme, the if statement
is made up of three parts. The first part is the test, which determines how you will proceed through the rest of the if code.
The second part is the consequent, which is the code block executed if the test is true.
The third part is the alternative, which corresponds to an else statement
in other popular languages. This code block is executed if the test is false.
In the example above, a procedure called ifExample is created to show how the if statement works. For information about creating procedures in Scheme,
see Lambda.
In the body of the procedure is a simple if statement. The test in this
example is (>= x 0), the consequent is (+ x 1), and the alternative is (- x 1).
When passed a number, the ifExample procedure will increment the number if it is greater than or equal
to 0 and decrement it if it is a negative number. Therefore, the function
call (ifExample 1) will return 2 and the function call (ifExample -1) will return -2.
(define halve (lambda (x) (/ x 2))) (map halve (list 2 4 6 8 10))
The Scheme map statement
applies a desired function to each element of a list. For more information
on lists, see Pairs and Lists.
The example above uses a function called halve. For more information on creating functions with
a lambda statement, see Lambda.
In the example above, the function halve is created to divide any number passed to it by 2.
In the second line, a map statement is used to apply
the halve function to each number of the
list (list 2 4 6 8 10). This map statement
returns (1 2 3 4 5).
The map statement can be used with essentially any function as long as that function is compatible with the list items on which it will operate.