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

From: aditya shukla (adityashukla1983 at gmail.com)
Date: Thu Feb 26 14:13:03 EST 2009

I was just solving  this exercise which i found online

*b)* Using the image.ss teachpack in DrScheme, we can write functions that
process graphic images. Here is some sample code:

;; Traffic Light example, HtDP Section 6.2, Figure 8
;; Revised to use image.ss teachpack by Matthias Felleisen
(define WIDTH 50)
(define HEIGHT 160)
(define RADIUS 20)
(define OFFSET 50)

(define RED (circle RADIUS 'solid 'red))
(define YELLOW (circle RADIUS 'solid 'yellow))
(define GREEN (circle RADIUS 'solid 'green))

(define LIGHT-FRAME (rectangle WIDTH HEIGHT 'outline 'black))

(define ALL-THREE-ON
  (overlay/xy (overlay/xy (overlay/xy LIGHT-FRAME 0 OFFSET GREEN)
                          0 0 YELLOW)
              0 (- OFFSET) RED))

"All three lights on!"


*(b.2)* Write the function turn-off-red 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.

If you test this with (turn-off-red ALL-THREE-ON), you should see a traffic
light with the yellow and green lights on. Do that, and then just evaluate
the symbol ALL-THREE-ON again. Its value didn't change; it still shows all
three colors. Nothing we've done changes the value we associated with
ALL-THREE-ON; we just used that value as input to a function, which returned
another image, another value based on that input.

If you have experience programming in other languages, you might have
expected ALL-THREE-ON to change. That's because your previous language
probably uses the *imperative programming* style, which generally relies on
changing the values of variables. But in Scheme, we're programming in
the *functional
programming* 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.

This is what i came up with
;A traffic light is an image which has a light frame , red , yellow and
green lights.
;contract traffic-light -> traffic-light(red light turned off)
;purpose this function takes a traffic light as input and returns a
new-traffic light with red-light turned off.
(define turn-off-red (lambda(traffic-light)
                       (overlay/xy (overlay/xy (overlay/xy LIGHT-FRAME 0
                          0 0 YELLOW)
              0 (- OFFSET) (circle RADIUS 'outline 'white) )))

(turn-off-red ALL-THREE-ON)

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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090226/4d0fccca/attachment.html>

Posted on the users mailing list.