[plt-scheme] Scheme exercise(image.ss)

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Thu Feb 26 23:18:26 EST 2009

aditya shukla wrote:
> I came up with this
> 
> (define turn-off-red (lambda(traffic-light)
>                    (overlay/xy (overlay/xy  LIGHT-FRAME 0 0 
> traffic-light) 0 (- OFFSET) (circle RADIUS 'solid 'white)) ))
> 
> (turn-off-red (overlay/xy (overlay/xy (overlay/xy LIGHT-FRAME 0 OFFSET 
> GREEN )
>                           0 0 (circle RADIUS 'outline 'white))
>               0 (- OFFSET) RED ))
> (define traffic-light (overlay/xy (overlay/xy (overlay/xy LIGHT-FRAME 0 
> OFFSET GREEN )
>                           0 0 (circle RADIUS 'outline 'white))
>               0 (- OFFSET) RED ))
> 
> traffic-light
> 
> (turn-off-red ALL-THREE-ON)
> 
> ALL-THREE-ON
> 
> I think i got this now.

You program may produce the correct result, but I'm not convinced you've 
mastered this problem.  You can convince us by making a bunch of 
examples and showing that your program does the right thing for these 
examples.  Of course, this means you have to know what the "right thing" 
is.  You should not start solving a problem until you know what it means 
to have a solution, otherwise you are doomed to fail.

Write down expressions to produce traffic lights with various parts on:

(define RED-ONLY ...)
(define GREEN-ONLY ...)
(define RED-GREEN ...)
(define GREEN-YELLOW ...)

Use ALL-THREE-ON as a guide to help you write these expressions.

Now for each one of these, write down expressions to produce what these 
lights would look like if you turned off the red light.  You can't use 
turn-off-red to do this!  But you can you use the original light if you 
want (in fact, it will help if you do).

(define RED-ONLY/RED-OFF ... RED-ONLY ...)
(define GREEN-ONLY/RED-OFF ... GREEN-ONLY ...)
(define RED-GREEN/RED-OFF ... RED-GREEN ...)
(define GREEN-YELLOW/RED-OFF ... GREEN-YELLOW ...)

At this point, you've solved the problem for 4 particular instances. 
How do you solve the problem for ANY traffic light?  Use the particulars 
to guide your general solution: turn-off-red.

Test your general solution by verifying it handles your particular cases:

(check-expect (turn-off-red RED-ONLY) RED-ONLY/RED-OFF)
...

If you follow this process of making examples, solving particular 
instances of the problem at hand, and ONLY THEN trying to solve the 
problem in general, you will arrive at the correct solution much faster 
than just diving into coding.  Afterward, you can verify your solution 
against the things you worked out by hand.  You won't have to ask us if 
the solution is correct --- you will know it is.

Finally, let me mention a couple tools to make your life easier:

1) Auto-indent: select a section of code and hit tab.  This will lay out 
your code properly making it easier for you and others to read.  Try to 
make your code look like code in the book; where does the book put a 
newline?  where does the book put white space?

2) The Stepper: See the foot that says "Step"?  Write down some examples 
and press that.  It will take you through the steps that DrScheme takes 
to run your program.  Try to make a model of how this works in your 
head.  Predict the steps that will be taken.  Again, think of this as 
algebra.  (+ 1 1) steps to 2.  What does (circle ...) step to?  What 
does (overlay/xy ...) step to?  Think, then experiment.

David



Posted on the users mailing list.