在 Haskell 中,我们有
在 PureScript 中,我们有
这里很明显,
以以下示例为例:
IO
单子用于处理副作用,但它不能表达副作用,并且你不知道真正发生了什么类型的副作用。main :: IO ()
在 PureScript 中,我们有
Eff
Monad,根据类型签名,您可以知道发生了哪种类型的副作用。main :: forall e. Eff (fs :: FS, trace :: Trace, process :: Process | e) Unit
这里很明显,
main
函数使用了文件系统、将消息追踪到控制台并能够处理当前进程。我们有一个特定的模块Control.Monad.Eff
来处理副作用以及子模块,如Control.Monad.Eff.Random
和Control.Monad.Eff.Console
。以以下示例为例:
module RandomExample where
import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Random (random)
import Control.Monad.Eff.Console (print)
printRandom :: forall e. Eff (console :: CONSOLE, random :: RANDOM | e) Unit
printRandom = do
n <- random
print n
这比仅使用"嘿,这里发生了一个副作用,就这样,你不需要知道更多!"要具体得多。我在网上搜索过,没有看到足够完整的单子来跟踪副作用。
Haskell中是否有特定的monad,如Eff
,可用于跟踪副作用?
谢谢您提前。
IO
有效果,而不是副作用。副作用会是一些邪恶的事情,比如通过unsafePerformIO
进行 I/O 的计算假装是纯洁的。 - Cactus