out the let* versus the set!
construct.
This much is clear. let* is
for creating local variables
and Set! is for creating global
variables (as well as setting
the value of variables).
It's the parentheses I don't
understand. For example, here's
a tutorial where I'm a little
confused about the use of parentheses
when using let*:
Variables And Functions
In the tutorial, I see the following
lines of code:
(let*
(
(a 1)
(b 2)
)
(+ a b)
)
It's as if the first argument to
the let* construct is all
the variables you plan to set. After
this one argument is taken care of,
all the arguments that follow are
basically program statements.
Is that all it is? That's how it
appears to me. I've gone and looked
at several examples of let* and
it appears that when you create local
variables, an extra set of parentheses
is necessary to make your new variables
all hang together as one argument to
let*.
That seems to be the key to understanding
let*, at least in my mind, See the
first argument to let* as variables
being defined and see all other arguments
as program statements that execute in sequence.
If this is the case, then basically let*
is a namespace mechanism. It is a mechanism
for creating a block namespace. Inside the
block, every variable created there is local.
In other words, let* is block scope.
Block scope is something that will be familiar
to C Language programmers. It is a scope that
is more local than function scope.
Block scope is valuable. It gives you a scratchpad
where you can work something out without having
repercussions that effect the rest of your program.
Block scope allows you to manipulate variables without
worrying about effects that might occur outside
the block.
This is starting to make sense. It seems to me that
Scheme is a very intellectually pure language. Part
of the purity is that everything is in prefix notation.
That is to say, the operator always comes first.
If you are going to do things that way then perhaps
a let* construct is the only reasonable
way to create little tiny work areas where things
get done. That is to say, a let* construct
may be the only reasonable way to segregate little
tiny scratchpad work areas, one from another.
The lesson. The more things change, the more things
stay the same. To all appearances, Scheme is unlike
any other programming language that I have worked with
and used.
However, as I scratch around a bit and get below the
surface it is beginning to seem as if things are very
much the same after all.
The above tutorial summarizes the let* very
nicely:
(let* ( variables )
expressions )
That's a very nice way to summarize it. It makes it
clear that the first argument to let* is variables
and every other argument is an expression of some kind.
It makes it clear by unwinding the parenthesis. The
parenthesis in Scheme will drive you crazy if you let
them.
This reminds of how to learn something new. One of
the first steps in learning something new is to make
it simple. So often, making something simple is a matter
of making a few simple observations.
Ed Abbott
No comments:
Post a Comment