functional-programming - 在 SMLofNJ.Cont 中隔离

标签 functional-programming sml smlnj continuation-passing

我在 Standard ML (SMLofNJ.Cont) 中阅读有关continuations的内容。我了解 callccthrow 的作用,但无法理解 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 使用 callccthrow:

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 signatureSML/NJ's CONT signature 有不同的文档行:

  • isolate f creates a continuation that evaluates f 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/

相关文章:

haskell - 无法匹配预期的 Haskell 函数类型

c++ - 这里如何正确使用for_each?

functional-programming - 新的 OCaml : How would I go about implementing Gaussian Elimination?

c# - 如何使用 STAMP 加密读取 tbf 文件

haskell - 将 ML 风格的模块添加到 Haskell 的主要理论困难是什么?

.net - F#中如何实现二叉搜索树的add操作?

recursion - 如何在sml中递归地重复调用另一个函数?

recursion - "continuations"在函数式编程中是什么意思?(特别是 SML)

sml - SML 中的 If Else 中的多个语句

functional-programming - 如何更新SML中的记录值?