Finger Exercises on State Changes

external ~<#48220#>Many of these exercises require solutions from previous (extended) exercises. The students can often do the exercises here even if they didn't solve those from previous sections by using our solutions. This requires students to read programs and to understand them.<#48220#> <#48221#>Exercise 37.4.1<#48221#> Modify <#68782#><#48223#>check-guess-for-list<#48223#><#68782#> from exercise~#exguess3#48224> so that it also counts how many times the player has clicked on the ``Check'' button of the interface. <#48225#>Hint:<#48225#> \ The call-back function for the button uses <#68783#><#48226#>check-guess-for-list<#48226#><#68783#> once per click.~ external Solution<#68784#><#68784#> <#48232#>Exercise 37.4.2<#48232#> Develop a program that manages a task queue. The program should support at least four services:
  1. <#68785#><#48235#>enter<#48235#><#68785#>, which adds a task to end of the queue;
  2. <#68786#><#48236#>next<#48236#><#68786#>, which determines the next task in the queue, if any;
  3. <#68787#><#48237#>remove<#48237#><#68787#>, which removes the first task from the queue, if any;
  4. and <#68788#><#48238#>count<#48238#><#68788#>, which computes the number of items in the queue.
A user should start the task manager with <#68789#><#48240#>start-task-manager<#48240#><#68789#>. After the program is developed and tested, use <#48241#>gui.ss<#48241#> to develop a graphical user interface to the task manager. The interface should start up with a friendly message and should always display the first task in the queue and the number of items in the queue: rawhtml58 Unless the queue is empty, clicking the ``Next'' button should remove an item from the queue and display the first item in the remainder of the queue. If the queue is empty, clicking the ``Next'' button should have no effect. <#48242#>Hint:<#48242#> \ The greeting and the year are two separate message items.~ external Solution<#68790#><#68790#> <#48248#>Exercise 37.4.3<#48248#> In section~#secmovefig#48250>, we developed a program that moves pictures across a canvas. A picture is a list of shapes; the program consists of functions that draws, erases, and translates pictures. The main function is <#68791#><#48251#>move<#48251#><#68791#> (exercise~#exmoving#48252>). It consumes a picture and a number <#68792#><#48253#>n<#48253#><#68792#>. It produces a new picture, moved by <#68793#><#48254#>n<#48254#><#68793#> pixels; it also erases the original picture and draws the new one. Develop the program <#68794#><#48255#>drive<#48255#><#68794#>. It draws a (fixed) picture on a canvas and permits players to move the picture left or right by a player-specified number of pixels. Modify <#68795#><#48256#>drive<#48256#><#68795#> so that it also keeps track of some given amount of fuel. A move by one pixel should consume a fixed amount of fuel.~ external Solution<#68796#><#68796#> <#48262#>Exercise 37.4.4<#48262#> Modify the two functions that control the state of a single traffic light so that they control the state of the two traffic lights at an ordinary intersection. Each light can assume one of three states: <#68797#><#48264#>'<#48264#><#48265#>red<#48265#><#68797#>, <#68798#><#48266#>'<#48266#><#48267#>green<#48267#><#68798#>, and <#68799#><#48268#>'<#48268#><#48269#>yellow<#48269#><#68799#>. When one is <#68800#><#48270#>'<#48270#><#48271#>green<#48271#><#68800#> or <#68801#><#48272#>'<#48272#><#48273#>yellow<#48273#><#68801#>, the other one must be <#68802#><#48274#>'<#48274#><#48275#>red<#48275#><#68802#>. Recall the basics about the two functions for a single traffic light:
<#71826#>;; <#68803#><#48280#>init-traffic-light<#48280#> <#48281#>:<#48281#> <#48282#><#48282#><#48283#>-;SPMgt;<#48283#><#48284#><#48284#> <#48285#>void<#48285#><#68803#> <#71826#>
<#71827#>;; effects: (1) to initialize <#68804#><#48286#>current-color<#48286#><#68804#>; (2) to draw traffic light<#71827#> 
and
<#71828#>;; <#68805#><#48294#>next<#48294#> <#48295#>:<#48295#> <#48296#><#48296#><#48297#>-;SPMgt;<#48297#><#48298#><#48298#> <#48299#>void<#48299#><#68805#><#71828#>
<#71829#>;; <#48300#>effects<#48300#>: (1) to change <#68806#><#48301#>current-color<#48301#><#68806#> from <#68807#><#48302#>'<#48302#><#48303#>green<#48303#><#68807#> to <#68808#><#48304#>'<#48304#><#48305#>yellow<#48305#><#68808#>, <#71829#> 
<#71830#>;; <#68809#><#48306#>'<#48306#><#48307#>yellow<#48307#><#68809#> to <#68810#><#48308#>'<#48308#><#48309#>red<#48309#><#68810#>, and <#68811#><#48310#>'<#48310#><#48311#>red<#48311#><#68811#> to <#68812#><#48312#>'<#48312#><#48313#>green<#48313#><#68812#><#71830#> 
<#48314#>;; (2) to re-draw the traffic light appropriately <#48314#> 
Modify the basics first. When the program is developed and tested, develop a graphical display like the following: rawhtml59 Use the functions <#68813#><#48318#>init-traffic-light<#48318#><#68813#> and <#68814#><#48319#>next<#48319#><#68814#> to drive the display, but keep the functions that display information separate from these two functions.~ external Solution<#68815#><#68815#> <#48325#>Exercise 37.4.5<#48325#> In sections~#secinterpreter#48327> and~#secinterpreter2#48328> we developed an evaluator for a portion of Scheme. A typical Scheme implementation also provides an <#68816#><#48329#>INTERACTIVE<#48329#><#68816#> user mode. In DrScheme, the <#48330#>Interactions<#48330#> window plays this role. An interactive system prompts the reader for definitions and expressions, evaluates them appropriately, and possibly produces some result. Definitions are added to some repository; to confirm the addition, the interactive system may produce a value like <#68817#><#48331#>true<#48331#><#68817#>. Expressions are evaluated relative to the definition repository. The function <#68818#><#48332#>interpret-with-defs<#48332#><#68818#> from section~#secinterpreter2#48333> performs this role. Develop an interaction system around <#68819#><#48334#>interpret-with-defs<#48334#><#68819#>. The system should provide at least two services:
  1. <#68820#><#48336#>add-definition<#48336#><#68820#>, which adds (the representation of) some function definition to the system's repository;
  2. <#68821#><#48337#>evaluate<#48337#><#68821#>, which consumes (the representation of) some expression and evaluates it relative to the current repository.
If a user adds two (or more) definitions for some function <#68822#><#48339#>f<#48339#><#68822#>, the last addition is the one that matters. The previous ones can be ignored.~ external Solution<#68823#><#68823#>