[Haskell-cafe] functions making their own memos

Greg Horn gregmainland at gmail.com
Sun Jul 8 11:15:51 UTC 2018

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 at gmail.com>

> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180708/d9eee454/attachment.html>

More information about the Haskell-Cafe mailing list