In this short video, I want to show you how to use the stepper, which is
functionality built into DrRacket that can help you understand the step-by-step
evaluation of complex expressions. I have the stepper-starter.rkt file open
here, and if we look at this file, there's one expression here athe top, a
simple expression, and then a function definition and a call to that function.
If we look at this expression, let's see, plus times 3 2, that's 6 plus 1, that's
7, that's easy enough to see. And here we're calling max-dim with a
rectangle. And there's an if, and it may be a little
bit harder to quickly see what the result is here.
Let's run it. Let's see that produced seven and the
call to the function produced 20, and you could try to work through this and see
why it's 20. And maybe you can see that quickly.
Or maybe you can't. What the step is for is to help you
understand how the evaluation re-expression [/g] produced a certain
value. In cases where it's too complicated to
just see. So let's try writing the stepper.
I'll say step here. And I'll make this window A little bit
bigger. And what the stepper does is it goes
through the file and it follows the beginnings to the language evaluation
rules. So the require doesn't produce a value.
The require is just a declaration, so, on the left-hand side, we see What brackets
evaluating on the right hand side, we see the current, the result of the current
evaluation step. So let's see, try to evaluate plus, open
paren plus, but the first operand is not a value, so it has to be evaluated.
Open param times. Both operands are values so this
expression can now be evaluated. That's what's racket [/g] is telling us
in green. Is that this expression is ready to be
evaluated. And it produces six.
If I say step again, then what's on the right here Moves to the left.
And in green, Racket is telling us okay, this whole expression is ready to be
evaluated. All of the operands are already values
and when I call plus on 6 and 1 I get 7. If I say Step again, now Racket says, hey
it's. Seeing this function definition and
that's just a definition nothing happens to it.
Its also seen a call to that function max dim.
The call isn't ready to be evaluated yet because one of the operands the only
operand is not yet a value. But that operand itself is ready to be
evaluated because all of its operands are values and when it evaluates rectangle of
10, 20 solid blue. The result produced by that is the
rectangle. It's, it doesn't quite look blue here,
because Racket is highlighting it for us. But the call to rectangle with those
arguments, produces a blue rectangle, so now over here on the left, we have
max-dim of the blue rectangle. Again, it doesn't quite look blue because
of the green highlight. [INAUDIBLE] And what's the rule for
calling a function? Well the rule for calling a function is
to replace the call to the function by the body of the function, this stuff
here. Where every occurrence of the parameter,
in this case, IMG, has been replaced by the argument, in this case, the blue
rectangle. So that's what we've got here, one
evaluation step takes to max dim of the blue rectangle to this whole expression.
If we say step again, now Racket is trying to evaluate the if Expression.
It can't do that, because the question is not yet a value, so it wants to evaluate
the greater than. It can't do that because the first
operand is not yet a value. It wants to evaluate the call to image
width. It can do that because the first operand
is a value that produces 10. Similarly [/g] now we can do the call to
image height. Which produces 20.
Now it can do the greater than 10 20 because both operands are values.
That produces false. Now the rule for an If is that if the
question is the value false, then the entire If gets replaced by the false
answer expression. That's what's happening here, here's the
false answer expression. And the entire if gets replaced with that
here. Now Racket can evaluate this call to
image height because the operand is a value.
It now produces 20 and that's the end of the evaluation.
You can also go backwards if you want to, just to go back to some earlier point.
So that's how the stepper works. And what the stepper is always showing
you is its always showing you whats the innermost expression where all the
operands Our values. And on the right it's showing you,
highlighted in pink, or purple I guess, what the, value of that expression is.
And you can step backwards and forwards to understand this.
I'd encourage you to learn how to use the stepper.
As our programs get more and more complicated, the stepper can be very
useful, particularly if, for example, when you have a function which isn't
producing the value that you wanted it to.
You can then step through it and see why its producing the value that it does.