CPS协程:为什么此示例需要精疲力尽?

问题描述 投票:0回答:1

我正在尝试从wikibooks / HaskellCPS章中找出the coroutines example,但是我不明白为什么runCoroutineT函数最后需要. (<* exhaust),如果我更改了函数来自

runCoroutineT = flip evalStateT [] . flip runContT return . runCoroutineT' . (<* exhaust)

to

runCoroutineT = flip evalStateT [] . flip runContT return . runCoroutineT'

该示例似乎为keep working(看一下第56行)任何人都可以解释是否有错误吗?

haskell monads coroutine continuations
1个回答
1
投票

在我看来,它不起作用。您链接到的示例程序以[]结尾

example = runCoroutineT $ do
    fork $ replicateM_ 3 (printOne 3)
    fork $ replicateM_ 4 (printOne 4)
    replicateM_ 2 (printOne 2)

但是运行时,不是所有的4都打印出来:

3
4
3
2
4
3
2
4

应该有四个!

exhaust的要点是结束所有等待运行的线程,而如果没有它,则可能无法完成某些线程的运行。

© www.soinside.com 2019 - 2024. All rights reserved.