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

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Thu Feb 26 22:30:51 EST 2009

aditya shukla wrote:
> 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 
> OFFSET GREEN)
>                           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.

Aditya,

Think of this as ALGEBRA WITH IMAGES.  Your function is given a traffic 
light image and ADDS an image to it.  In particular, it adds a white 
circle image in the position of the red light of the given traffic light.

Analogously, think about ALGEBRA WITH NUMBERS.  Suppose you want a 
function that takes a number and adds a number to it.  In particular, it 
adds the number 7 to the given number.

;; add 7 to the given number
;; Number -> Number
(define (add7 n) ...)

Now suppose you had only one example: (add7 2) --> 9.

I could get the "right" answer with this:

(define (add7 n) 9)

See, all tests pass!:

(check-expect (add7 2) 9)

Of course, this is not right if we consider other examples.  Your 
solution is doing exactly the same sort of thing only with images.

Try to correct my bad add7 program.  How did you fix it?  Now think 
about the analogy between the algebra of numbers and the algebra of 
images.  What is like n?  What is like +?  Fix your program just like 
you fixed mine.

David



Posted on the users mailing list.