Stack usage with a state monad
Joe Thornber
thornber at sistina.com
Wed Dec 31 15:37:57 EST 2003
On Wed, Dec 31, 2003 at 02:38:06PM +0000, Graham Klyne wrote:
> getOrCachePositionValue pos =
> do { mcache <- gets (findPos pos) -- Query cache for position
> ; case mcache of
> Just cached -> return (cachedVal cached) -- Return cached value
> Nothing -> -- Not in cache:
> do { let val = calculatePosVal pos -- Calculate new value
> ; modify (addToCache pos val) -- Cache new value
> ; return val -- Return new value
> }
> }
>
> (This code of off-the-cuff, and may contain errors)
>
> My point is that the function 'calculatePosVal' used here to evaluate a
> position not in the cache simply returns the calculated value, not a
> monad. This function is wrapped in "high level" code that queries and/or
> updates the cache which is kept in a state monad. Thus, the return type of
> 'getOrCachePositionValue' would be a monad of the appropriate type.
But I want calculatePosVal to use the cache too :(
- Joe
More information about the Haskell-Cafe
mailing list