[racket] Advice: simulating stateful API in functional setting

From: Mark Engelberg (mark.engelberg at gmail.com)
Date: Sat Jul 16 00:34:15 EDT 2011

On Fri, Jul 15, 2011 at 12:24 PM, Nadeem Abdul Hamid <nadeem at acm.org> wrote:
>  Why don't you just have the picture generated based on the state of the
> sprite each tick of the animation, just as in HtDP "world"? Functions like
> move, rotate, etc. produce a modified sprite, and when that sprite gets
> passed to the draw handler, the draw handler produces a new image of the
> world based on that state.

Because the image is not only dependent upon the current location of
the sprite, but also the entire history of the sprite's movements when
its pen was down.  So you either have to store a very long list of
line segments in the world structure, redrawing them every time, or
alternatively, the world structure is comprised of the current sprite
info and a picture of what has been drawn thus far.

My instinct is to go for the sprite+picture world representation.  The
draw handler just draws the picture held in the world representation
overlaid with the sprite at its desired location.  The other movement
functions both update the sprite part of the world struct and the
picture part of the world struct (when the pen is down).



Posted on the users mailing list.