为了熟悉unsafePerformIO(如何使用它以及何时使用它),我实现了一个用于生成唯一值的模块.
这就是我所拥有的:
module Unique (newUnique) where
import Data.IORef
import System.IO.Unsafe (unsafePerformIO)
-- Type to represent a unique thing.
-- Show is derived just for testing purposes.
newtype Unique = U Integer
deriving Show
-- I believe this is the Haskell'98 derived instance, but
-- I want to be explicit, since its Eq instance is the most
-- important part of Unique.
instance Eq Unique where
(U x) == (U y) = x == y
counter :: IORef Integer …Run Code Online (Sandbox Code Playgroud) 我知道它听起来一定是微不足道的,但我想知道如何从仿函数中展开一个值并将其作为纯值返回?
我试过了:
f::IO a->a
f x=(x>>=)
f= >>=
Run Code Online (Sandbox Code Playgroud)
我应该在右侧放置什么?我不能使用,return因为它会再次包裹它.