<div dir="ltr"><div>I've been wondering for some time how to handle a common use case in imperative programming, "static" variables that are accessed only inside one function, but not needed elsewhere.</div><div><br></div><div>I think this probably has something to do with existential quantification, but in any case this is an area I'm ready to learn a lot more about. Any ideas are welcome along with suggested reading.<br></div><div><br></div><div>Let's say that I have a function for generating random numbers every time it is called, but I sometimes want it to to re-use the last number depending on the time the last number was geneated:<br></div><div><br></div><div>sometimesNew :: Time -> Int -> IO (Time,Int)</div><div>sometimesNew lastTime lastValue = do</div><div>  currentTime <- <get time for system></div><div>  if currentTime > lastTime + 10</div><div>    then do</div><div>      newValue <- <get pseudorandom value></div><div>      return (currentTime,newValue)</div><div>    else return (lastTime,lastValue)</div><div><br></div><div>This function depends on the caller to remember its output (last time and last value) and feed that back to it. <br></div><div><br></div><div>It would be nice if this function could make some kind of memo inside it itself and no one else needs to know.</div><div><br></div><div>The case I'm working on right now is doing animation in Purescript on a canvas, in which I can control the motion of an object appearing in the animation by composing functions that control the motion. I might like to write a function that generates an overall circular trajectory, then compose it with a function that generates a slight wobbly motion. The wobbly function needs, basically, to change the motion randomly by interpolating between random positions and changing the random position only occassionally instead of during every animation frame.</div><div><br></div><div>type MovementFunc = ...</div><div><br></div><div>So this would generate a large circular movement:</div><div><br></div><div>circularMovement :: MovementFunc</div><div><br></div><div>This generates a small wobble.</div><div><br></div><div>wobble :: MovementFunc</div><div><br></div><div>Then I can compute the final position once per animation frame by superimposing or composing individual movements:<br></div><div><br></div><div>computePosition :: [MovementFunc] -> Time -> IO Position</div><div><br></div><div>The wobble function might like to have access to a memo of the last randomly chosen position, and how long ago in milliseconds it chose that position.</div><div><br></div><div>Let's say that every movement function might like to have access to a memo, but the actual data type involved could be different from function to function. I also need to deal with initializing the memo. So the trick is to maintain and initialize these memo types without needing to know the internals of every function.</div><div><br></div><div>Dennis<br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div>