I was just solving this exercise which i found online<br><br><br><font face="Goudy Old Style"><b>b)</b> Using the image.ss teachpack in
DrScheme, we can write functions that process graphic images. Here is some
sample code:<a name="_14_3453"></a></font>
<pre><font face="Courier New, Courier, monospace"><br>;; Traffic Light example, HtDP Section 6.2, Figure 8<br>;; Revised to use image.ss teachpack by Matthias Felleisen<br>(define WIDTH 50)<br>(define HEIGHT 160)<br>(define RADIUS 20)<br>
(define OFFSET 50)<br><br>(define RED (circle RADIUS 'solid 'red))<br>(define YELLOW (circle RADIUS 'solid 'yellow))<br>(define GREEN (circle RADIUS 'solid 'green))<br><br>(define LIGHT-FRAME (rectangle WIDTH HEIGHT 'outline 'black))<br>
<br>(define ALL-THREE-ON<br> (overlay/xy (overlay/xy (overlay/xy LIGHT-FRAME 0 OFFSET GREEN)<br> 0 0 YELLOW)<br> 0 (- OFFSET) RED))<br><br>"All three lights on!"<br><br>ALL-THREE-ON<br>
</font></pre><br><p>
<font face="Goudy Old Style" size="2"><b>(<font size="4">b.2)</font></b><font size="4"> Write the function </font></font><font face="Courier New" size="4">turn-off-red</font><font face="Goudy Old Style" size="4">
that takes a traffic light image as its input and returns a traffic light
image that's the same as the input, except that the red-light position
is off (i.e., there's a white circle in the top position). You don't
have to check whether the red light (or any other light) is on to start
with.</font></p>
<p>
<font face="Goudy Old Style" size="2">If you test this with</font><font face="Courier New" size="2">
(turn-off-red ALL-THREE-ON)</font><font face="Goudy Old Style" size="2">, you should
see a traffic light with the yellow and green lights on. Do that, and then
just evaluate the symbol </font><font face="Courier New" size="2">ALL-THREE-ON</font><font face="Goudy Old Style" size="2">
again. Its value didn't change; it still shows all three colors. Nothing
we've done changes the value we associated with </font><font face="Courier New" size="2">ALL-THREE-ON</font><font face="Goudy Old Style" size="2">;
we just used that value as input to a function, which returned another image,
another value based on that input.</font></p>
<p>
<font face="Goudy Old Style" size="2">If you have experience programming in other
languages, you might have expected </font><font face="Courier New" size="2">ALL-THREE-ON</font><font face="Goudy Old Style" size="2">
to change. That's because your previous language probably uses the
<b>imperative programming</b> style, which generally relies on changing
the values of variables. But in Scheme, we're programming in the <b>functional
programming</b> style. This means that we're not changing the values
of variables; instead, we're just sending values as inputs to functions,
which return other values. You may wonder how it's even possible to
write programs without assigning new values to variables. Well, just stay
tuned, and for the moment just remember that the functions we write don't
change the values of their inputs.</font></p><p>This is what i came up with <br></p>;A traffic light is an image which has a light frame , red , yellow and green lights.<br>;contract traffic-light -> traffic-light(red light turned off)<br>
;purpose this function takes a traffic light as input and returns a new-traffic light with red-light turned off.<br>(define turn-off-red (lambda(traffic-light)<br> (overlay/xy (overlay/xy (overlay/xy LIGHT-FRAME 0 OFFSET GREEN)<br>
0 0 YELLOW)<br> 0 (- OFFSET) (circle RADIUS 'outline 'white) )))<br><br><br><br>(turn-off-red ALL-THREE-ON)<br><br>This gave me the desired output , but i am wondering did i do it the right way, ie , not changing the value of the variable which is input to the function and returning another value.I think i did it right but will appreciate if someone can see if it's done in the right way.<br>
<br><br><br>Aditya<br><p><br></p><br><p><br></p><p><br></p><p><br></p><p><font face="Goudy Old Style" size="2"><br></font></p>