Scheme Programming/Using Variables
[edit] Using Variables
In order to make your program a little more dynamic, it is often necessary to declare a variable, there are 2 main ways of doing this, using define and let. The difference between the two is simple once fully grasped.
For example, using define:
> (define x 2) #<unspecified> > (* x 3) 6 > x 2
This shows that even once used, the variable x continues to exist, it is defined for all the see, use and (as we shall see later on) modify at will.
In general, the form of a define statement for a variable is as follows:
(define <variable name> <variable value>)
Now we will see the use of let
> (let ((x 2) (y 4)) (* x y)) 8 > x ;ERROR: "/usr/lib/scm/Iedline.scm": unbound variable: x > y ;ERROR: "/usr/lib/scm/Iedline.scm": unbound variable: y
Notice here, how once the block of code has finished executing, the variables x and y are no longer accessible. This is a very bulky and slow way to do this at this point, but it allows for more elegant programs later.
In general, the form of a let statement is as follows:
(let ((var1 val1) ... (varn valn)) (<body>))
This page may need to be