问题描述
不太理解Haskell中并发和并行的上下文中的决定论。一些例子是有帮助的。
感谢
Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some examples would be helpful.Thanks
推荐答案
在处理纯值时,评估顺序无关紧要。这本质上是并行性的作用:并行地评估纯值。与纯粹的价值观相反,秩序通常对于具有副作用的行动很重要。同时运行操作称为并发。
When dealing with pure values, the order of evaluation does not matter. That is essentially what parallelism does: Evaluating pure values in parallel. As opposed to pure values, order usually matters for actions with side-effects. Running actions simultaneously is called concurrency.
例如,考虑 putStrfoo
和 putStrbar
。根据这两个动作得到求值的顺序,输出是foobar,barfoo或它们之间的任何状态。
As an example, consider the two actions putStr "foo"
and putStr "bar"
. Depending on the order in which those two actions get evaluated, the output is either "foobar", "barfoo" or any state in between. The output is indeterministic as it depends on the specific order of evaluation.
另一个例子,考虑两个值 sum [1..10]
和 5 * 3
。无论这两种方法的顺序如何,它们总是减少到相同的结果。这种确定论是你通常只能用纯值来保证的东西。
As another example, consider the two values sum [1..10]
and 5 * 3
. Regardless of the order in which those two get evaluated, they always reduce to the same results. This determinism is something you can usually only guarantee with pure values.
这篇关于为什么并发haskell非确定性,而并行haskell原语(par和pseq)确定性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!