1

I need to create a defintion that outputs a picture of a traffic light depending on a string either green yellow or red and whatever the string is determines which bulb is solid

(define green-light
  (overlay (above (circle 15 "solid" "green")
                  (circle 15 "outline" "yellow")
                  (circle 15 "outline" "red"))
           (rectangle 50 100 "outline" "black")))

(define yellow-light
  (overlay (above (circle 15 "outline" "green")
                  (circle 15 "solid" "yellow")
                  (circle 15 "outline" "red"))
           (rectangle 50 100 "outline" "black")))

(define red-light
  (overlay (above (circle 15 "outline" "green")
                  (circle 15 "outline" "yellow")
                  (circle 15 "solid" "red"))
           ( rectangle 50 100 "outline" "black")))

(check-expect (TrafficLightState "green")
              (overlay (above (circle 15 "solid" "green")
                              (circle 15 "outline" "yellow")
                              (circle 15 "outline" "red"))
                       (rectangle 50 100 "outline" "black")))

(check-expect (TrafficLightState "yellow")
              (overlay (above (circle 15 "outline" "green")
                              (circle 15 "solid" "yellow")
                              (circle 15 "outline" "red"))
                       (rectangle 50 100 "outline" "black")))
(check-expect (TrafficLightState "red")
              (overlay (above (circle 15 "outline" "green")
                              (circle 15 "outline" "yellow")
                              (circle 15 "solid" "red"))
                       (rectangle 50 100 "outline" "black")))


(define (TrafficLightState color) 
  (cond [(TrafficLightState "green") (place-image green-light)]
        [(TrafficLightState "yellow") (place-image yellow-light)]
        [(TrafficLightState "red") (place-image red-light)]))
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
  • It looks like this is homework related. Presuming you are taking a class based on "How to Design Programs" can you tell us what step of the design recipe you are on? – Leif Andersen Sep 12 '16 at 21:29
  • The header portion I just need to create a stoplight using shapes and I am having trouble my check-expects worked so I am confused as to why this didn't because its the same code – user6824158 Sep 12 '16 at 21:38
  • 1
    Oh? Okay, in that case I don't see any purpose statements. Can you please add them? – Leif Andersen Sep 12 '16 at 21:42

1 Answers1

2

The issue is in your TrafficLightState function. If you click the “Check Syntax” button and hover over its name in DrRacket, you’ll see something of a hint about what’s wrong with it:

All those arrows point to the places where the TrafficLightState function is used, and indeed, it’s being used three times inside itself. That is, the TrafficLightState function is calling itself, which is known as a recursive function.

Since TrafficLightState keeps calling itself, it effectively enters into an infinite loop, consuming more and more memory until it runs out. This definitely isn’t what you want, so you should reconsider the way cond works and adjust your function to fix it accordingly.

It might help to use the stepper in DrRacket to step through the execution of your program in order to understand exactly what’s going on. Click the “Step” button in the menu and click the arrows to move through the execution of your program one step at a time.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • 1
    I believe the stepper built into DrRacket might help you understand what's going on here. (... Said its author). – John Clements Sep 12 '16 at 22:21
  • @JohnClements Ah, I always forget about the stepper, mostly because I never actually went through HtDP, so it was never available to me. It’s really cool, though, so I’ll definitely add a note about it in my answer. – Alexis King Sep 12 '16 at 22:23
  • 1
    You should mention that in the teaching languages, those arrows are only visible when you hit the Check Syntax button – Alex Knauth Sep 12 '16 at 22:29
  • @AlexKnauth Ah, good point. Noted and mentioned in the answer. – Alexis King Sep 12 '16 at 22:30