monadicIO的工作方式 [英] How monadicIO works
问题描述
我有以下代码
fastShuffle :: [a] -> IO [a]
fastShuffle a = <some code>
prop_fastShuffle_correct :: [Int] -> Property
prop_fastShuffle_correct s =
monadicIO ( do
sh <- run (fastShuffle s)
return ( True ==> ( insertionSort sh == insertionSort s &&
if length s > 10
then s /= sh
else True ) ) )
..它正在工作.我不明白看起来像是纯函数( prop_fastShuffle_correct
)如何调用具有副作用( fastShuffle
)的非纯函数.
And .. it is working. I can't understand how what looks to be a pure function (prop_fastShuffle_correct
) can call a non pure function that has side effects (fastShuffle
).
希望有人可以解释.
谢谢!
推荐答案
Haskell中的函数永远不会有副作用.
Functions in Haskell never have side effects.
只有值具有副作用,例如 getLine
(这是一个值,而不是一个函数). getLine
是指令从标准输入中读取一行文本".它不是执行指令的函数,它是指令.
There are only values with side effects, like getLine
(which is a value, not a function). getLine
is the instruction "read a line of text from standard in". It's not a function that executes the instruction, it is the instruction.
putStrLn
不是将文本写到标准输出的函数. putStrLn
是一个函数,该函数将字符串作为参数,并返回一条指令以将该字符串写入标准输出.
And putStrLn
is not a function which writes text to standard out. putStrLn
is a function, which takes a string as a parameter, and returns an instruction to write that string to standard out.
将这些指令存储在纯数据结构中没有问题.但是,如果要实际执行它们,则在某些时候它们必须是主程序值 main
的一部分.
There is no problem with storing these instructions in pure data structures. If you want to actually execute them, however, then at some point they must be part of the main program value main
.
这篇关于monadicIO的工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!