Thursday, May 27, 2010

Learn to Program in
Scheme 3, The let* Construct

Ok. I'm now trying to figure
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

Tuesday, May 25, 2010

Learn to Program in
Scheme 2, Three Simple Observations

 
Here's a Scheme tutorial I
found on the web. The tutorial
is specific to Gimp. However,
it is mostly about Scheme.

Here's the tutorial:

Basic Scheme

I'll make 3 simple observations
about Scheme that I've gathered
from this tutorial:

  1. Everything in Scheme is surrounded
    by parentheses
  2. The first thing you find
    inside the left parenthesis is
    the operation you wish to perform
  3. Comments start with a semi-colon
    character (;)

Making these 3 observations goes a
long ways towards helping me to be
able to read Scheme. That's the first
thing you want to learn when learning
a new programming language. You want
to learn to read before you learn to
write.

Learning to read means learning to
recognize certain things right away.

The first thing I notice about the
script at the bottom of the page is
that many many of the lines start out
as comments. Comments are something
that help you to understand a program
but otherwise, have no bearing on the
actual execution of the program. In
other words, comments are not programming
at all, they are pure literature.

Look for all the semi-colons in the
script at the bottom of the page. If
you don't know what a semi-colon is,
it is a dot (period) on top and a comma
on the bottom. That's the general appearance.
A semi-colon appears as a dot on top of
a comma.

Comments are very helpful. You might want
to look for the semi-colons first and then
read all the comments that follow to the
right of the semi-colons.

Something that seems to characterize
Scheme is parentheses, parentheses,
parentheses. There are parentheses
everywhere.

I suspect that this is so because Scheme
basically views everything as a mathematical
operation. In math, you have the operation
and then you have the arguments to the
operation.

For example, as schoolchildren, we learn
this basic operation:

2 + 2 = 4

The plus sign is the operation and the
numbers that surround the plus sign are
the arguments to that operation. In
fact, I did a little too much. Not only
did I give the operation, I also gave the
result of the operation, the number 4.

What I should really do is just write this:

2 + 2

By only writing the operation and the arguments
to the operation, I'm giving my computer something
to do. It is going to find the answer for me.
Why would I ask my computer to do something for me
when I already know the answer?

So again, our basic operation is this:

2 + 2

In scheme, the operation would be written
like this:

(+ 2 2)

This is what I gather from the tutorial.
Operations are always surrounded by parentheses
and you give the operation before you give
the arguments. Here. I'll summarize this
as two rules:

  1. Operations are surrounded by parentheses
  2. The operator itself appears first inside
    the parentheses

OK. I think I'll leave it at that. From these
3 simple observations, a lot of information can
be extrapolated.

What's the lesson? Start small and build. To
learn a new programming language, you start small
and you build.

To learn Scheme, you make a few simple observations
and you come to as many conclusions as possible from
those simple observations. One observation leads to
another and one day, you know the language.

At least, that's how I've learned other programming
languages. So, I assume that this same simple method
will work with Scheme.

Ed Abbott

Monday, May 24, 2010

Learn to Program in
Scheme 1, Getting Started


This is the first post in
a new blog that is all about
scheme programming. Scheme,
as I understand it, is a subset
of Lisp, another programming language.

I'm not an expert in Scheme programming.
In fact, I'm learning the language
myself as I write.

Why am I learning Scheme? Because it
is a way to extend the Gimp, an image
editor. Gimp extensions are written
in Scheme.

In Gimp, you do most things by hand.
For example, lets say that we wish
to resize an image. Let's say we
wish to resize all images that are
more than 900 pixels wide to be just
900 pixels wide.

Of course, we can do this in Gimp
by resizing the images by hand. That
is to say, we can work with Gimp to
resize these images interactively.

Lets now say that we have one thousand
images we would like to resize. Doing
so interactively then becomes tedious
and error-prone.

This is where Scheme comes in. In Gimp,
you can use Scheme to resize the images
for you. That is to say, Gimp will load
the image, check to see if it is over 900
pixels wide. If it is over 900 pixels wide,
Gimp will scale it down to 900 pixels.

However, Gimp will not do this unless you
program Gimp to do it. To program Gimp,
you need a programming language. The
original language for doing such things
in Gimp is Scheme.

There are other languages you can use to
program Gimp. Python is becoming a more
and more viable option to Scheme if you
wish to program Gimp. However, as I write
in May of 2010, Scheme is still the most
popular way to do this. Also, Scheme
has been around the longest and is the
original scripting solution for Gimp.

Ed Abbott