[racket] Advice: simulating stateful API in functional setting

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Sat Jul 16 10:33:03 EDT 2011

Mark Engelberg wrote:

> I see you're already representing the World as a picture and a list of
> sprites, so just let the kids manipulate that directly.
> Since you want to support multiple sprites, what if each sprite in the
> sprite list had a name, so the contract is:
> move: String (sprite-name) Integer (distance) World -> World
> So:
> (define (zigzag sprite-name world)
>  (move sprite-name 10 (rotate-cw sprite-name 45 (move sprite-name 10
> (rotate-ccw sprite-name world)))))
> You could also provide functions to add and delete sprites from the world.
> add-sprite: String (sprite-name) Posn (start-location) Image World -> World
> delete-sprite: String (sprite-name) World -> World

These functions would presumably maintain the invariant that each name corresponds to at most one sprite in a world.  Letting the kids manipulate the list of sprites directly does NOT maintain that invariant, so I think I have to do one or the other: either show the kids the list and let them do what they will, or hide the list and provide only accessors to it.

Nonetheless, some version of this may be the least-bad idea so far, including mine :-)

Stephen Bloch
sbloch at adelphi.edu

Posted on the users mailing list.