Another way to think of a Scheme program is as a big machine made up of
a bunch of smaller machines. For example, to make a machine that does 4
+ 3 * 7, you would need one machine to multiply 3 by 7 and another
to add 4 to that.
|
|
Here is a machine that can add. It can take any number of arguments
at the top and returns one answer at the bottom. |
Here is a machine that can multiply. It can take any number of arguments
at the top and returns one answer at the bottom. |
Here is how you put them together to get a machine that calculates 4
+ 3 * 7:
Once you have the machine, you can write the Scheme code without too much
trouble. Start at the bottom and work your way up.
- At the very bottom is a + with a 4 coming in.
From this, you know that the Scheme code looks something like (+ 4 ...).
- The other thing coming into the + is the result from the
* machine. This means the code looks more like (+
4 (* ...)).
- The * machine has 3 and 7 coming in.
That's the end of the machine, so our Scheme code is (+ 4 (* 3
7)).
Now try sketching a machine that calculates square
root(32 + 42).