[Haskell-cafe] functions making their own memos

Dennis Raddle dennis.raddle at gmail.com
Sun Jul 8 22:09:51 UTC 2018

Thanks, that's a cool solution. Here's another thought to avoid IO. What if
we're operating inside a state monad which has a list of memos. The
creation function newCounter then adds a memo to the list and creates
getCount as a closure with that index. The problem is that when we define
the state with its list of memos, we don't know what data type each
individual function will want to use.


On Sun, Jul 8, 2018 at 4:15 AM, Greg Horn <gregmainland at gmail.com> wrote:

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

More information about the Haskell-Cafe mailing list