我在 Standard ML (SMLofNJ.Cont) 中阅读有关continuations的内容。我了解 callcc 和 throw 的作用,但无法理解 isolate 强>。文档说
Discard all live data from the calling context (except what is reachable from f or x), then call f(x), then exit. This may use much less memory then something like f(x) before exit().
但是这对我来说没有任何意义。我只是想通过一些示例了解此函数的作用。
最佳答案
MLton 做得更好 explaining an implementation isolate
使用 callcc
和 throw
:
val isolate: ('a -> unit) -> 'a t = fn (f: 'a -> unit) => callcc (fn k1 => let val x = callcc (fn k2 => throw (k1, k2)) val _ = (f x ; Exit.topLevelSuffix ()) handle exn => MLtonExn.topLevelHandler exn in raise Fail "MLton.Cont.isolate: return from (wrapped) func" end)
We use the standard nested callcc trick to return a continuation that is ready to receive an argument, execute the isolated function, and exit the program. [...]
该页面继续解释如何以更少的空间泄漏实现相同的效果。
MLton's CONT
signature与 SML/NJ's CONT
signature 有不同的文档行:
isolate f
creates a continuation that evaluatesf
in an empty context.This is a constant time operation, and yields a constant size stack.
关于functional-programming - 在 SMLofNJ.Cont 中隔离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53415239/