why is this code giving me a ran out of memory error code on racket? -
i need create defintion outputs picture of traffic light depending on string either green yellow or red , whatever string determines bulb 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)]))
the issue in trafficlightstate
function. if click “check syntax” button , hover on name in drracket, you’ll see of hint what’s wrong it:
all arrows point places trafficlightstate
function used, , indeed, it’s being used 3 times inside itself. is, trafficlightstate
function calling itself, known recursive function.
since trafficlightstate
keeps calling itself, enters infinite loop, consuming more , more memory until runs out. isn’t want, should reconsider way cond
works , adjust function fix accordingly.
it might use stepper in drracket step through execution of program in order understand what’s going on. click “step” button in menu , click arrows move through execution of program 1 step @ time.
Comments
Post a Comment