You can declare a variable using a !
form.
This takes a variable name, and an expression. It declares a new
variable with the given name, and gives it the value of the expression:
expression to the variable.
#|kawa:1|# (! binary-kilo 1024) #|kawa:2|# (! binary-mega (* binary-kilo binary-kilo)) #|kawa:3|# binary-mega 1048576
If you prefer, you can use define
instead of !
:
#|kawa:1|# (define binary-kilo 1024) #|kawa:2|# (define binary-mega (* binary-kilo binary-kilo)) #|kawa:3|# binary-mega 1048576
The advantage of using define
is that it is portable
to other Scheme implementations.
The advantages of using !
is that it is shorter;
it generalizes to patterns (see later);
and it guards against accidentally “shadowing” a variable
by a nested variable with the same name.
A !
(or define
) typed into the command-line
defines a top-level variable.
You can also declare local variables. NEED EXAMPLES.
Alternative forms for defining local variables are
let
, let*
, or letrec
/letrec*
.