为了熟悉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)