What about something like:

import Data.IORef ( IORef, newIORef, readIORef, writeIORef )

newCounter :: IO (IO Int)
newCounter = do
  counterRef <- newIORef 0 :: IO (IORef Int)
  let getCount :: IO Int
      getCount = do
        count <- readIORef counterRef
        writeIORef counterRef (count + 1)
        return count
  return getCount

The user calls this function which creates the cache and returns the stateful function. You can use MVars instead of IORefs if you want it to work concurrently.

On Sun, Jul 8, 2018 at 2:29 AM Dennis Raddle <dennis.raddle@gmail.com> wrote:
In this one use case, there might be a simpler solution, but I have encountered lots of situations in which a particular algorithm could benefit from an associated stored cache of information. The most natural way to write an algorithm, sometimes, is in terms of its past decisions or past state of the program, interacting with current state.

D
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.