相关疑难解决方法(0)

Haskell:通用的IORef,MVar?

我做了以下函数,该函数特定于IO monad:

memoIO :: MonadIO m => m a -> IO (m a)
memoIO action = do
  ref <- newMVar Nothing
  return $ do
    x <- maybe action return =<< liftIO (takeMVar ref)
    liftIO . putMVar ref $ Just x
    return x
Run Code Online (Sandbox Code Playgroud)

用法示例:

main :: IO ()
main = do
  p <- memoIO $ putStrLn "hello"
  p
  p
Run Code Online (Sandbox Code Playgroud)

打印" hello"一次.

我想(一个小小的烦恼)让它尽可能多地工作(不仅仅是IO).

我在hackage上发现了stateref,我的代码看起来像这样:

{-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, Rank2Types, UndecidableInstances #-}

import Data.MRef

class (NewMRef r m a, DefaultMRef r …
Run Code Online (Sandbox Code Playgroud)

haskell types ioref

6
推荐指数
1
解决办法
1578
查看次数

标签 统计

haskell ×1

ioref ×1

types ×1