集合算法的 Ocaml 实现建议

发布于 2024-08-13 06:24:31 字数 545 浏览 2 评论 0原文

我在 ocaml 中转换以下算法时遇到问题 为了实现这个算法,我使用了 Set.Make(String) 函子,实际上输入和输出是 2 个函数 任何人都可以在 ocaml 中为我提供精确的代码帮助,以便执行

以下操作实际上是实时变量算法[PDF] ..非常感谢您的帮助

for all n, in[n] = out[n] = Ø
w = { set of all nodes }
repeat until w empty
    n = w.pop( )
    out[n] = ∪  n’ ∈ succ [n] in[n’]
    in[n] = use[n] ∪ (out[n] — def [n])
    if change to in[n],
        for all predecessors m of n, w.push(m)
end

I am having problems for converting following algo in ocaml To implement this algorithm i used Set.Make(String) functor actualy in and out are 2 functions Can any one give me percise code help in ocaml for following

This is actually Algo for Live Variables[PDF] ..Help would be appreciated greatly

for all n, in[n] = out[n] = Ø
w = { set of all nodes }
repeat until w empty
    n = w.pop( )
    out[n] = ∪  n’ ∈ succ [n] in[n’]
    in[n] = use[n] ∪ (out[n] — def [n])
    if change to in[n],
        for all predecessors m of n, w.push(m)
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2024-08-20 06:24:31
for all n, in[n] = out[n] = Ø
    w = { set of all nodes }
    repeat until w empty
    n = w.pop( )
    out[n] = ∪  n’ ∈ succ [n] in[n’]
    in[n] = use[n] ∪ (out[n] — def [n])
    if change to in[n],
    for all predecessors m of n, w.push(m)
end

我很难说出这里究竟发生了什么。我认为您的文本存在一些对齐问题 --repeat Until wempty 应该重复接下来的 5 行,对吧? in 和 out 函数如何,对我来说它们看起来像数组?除了这些缺陷之外,我还将解决一些我遵循的一般规则。

我必须将 C 和 Fortran 算法中的许多数值方法翻译成函数式语言,我给你一些建议。

0) 定义所使用的数据类型。这将真正帮助您进行下一步(剧透:寻找功能结构)。一旦了解了数据类型,您就可以更准确地定义最终将应用的函数结构。

1)寻找功能性结构。折叠、递归和映射,以及何时使用它们。例如,所有前辈 m 的 是一个折叠(不确定它是否会折叠在树或列表上,但仍然是一个折叠)。 While 循环是递归的好地方——但不必担心使其成为尾调用,您可以稍后修改参数以遵守这些要求。不用担心是否 100% 纯净。删除足够多的不纯结构(引用或数组),以功能性的方式感受算法。

2) 尽可能地编写算法的任何部分。将函数留空,添加虚拟值,然后只实现你所知道的——然后你就可以提出更好、更明智的问题。

3)重写它。您很可能错过了一些函数构造或使用了数组或引用,现在您意识到可以使用列表或集合或通过传递累加器。您可能已经定义了一个列表,但后来您意识到您无法随机访问它(好吧,这将非常有害),或者需要前后遍历它(对于拉链来说是一个好地方)。不管怎样,当你最终得到它时你就会知道,你的脸上应该露出灿烂的笑容。

for all n, in[n] = out[n] = Ø
    w = { set of all nodes }
    repeat until w empty
    n = w.pop( )
    out[n] = ∪  n’ ∈ succ [n] in[n’]
    in[n] = use[n] ∪ (out[n] — def [n])
    if change to in[n],
    for all predecessors m of n, w.push(m)
end

It's hard for me to tell what is exactly going on here. I think there is some alignment issues with your text --repeat until w empty should be repeating the next 5lines, right? And how are in and out functions, they look like arrays to me? Aside from those deficiencies I'll tackle some general rules I have followed.

I've had to translate a number of numerical methods in C and Fortran algorithms to functional languages and I have some suggestions for you.

0) Define the datatypes being used. This will really help you with the next step (spoiler: looking for functional constructs). Once you know the datatypes you can more accurately define the functional constructs you will eventually apply.

1) Look for functional constructs. fold, recursion, and maps, and when to use them. For example, the for all predecessors m is a fold (unsure if that it would fold over a tree or list, but none-the-less, a fold). While loops are a good place for recursion --but don't worry about making it a tail call, you can modify the parameters later to adhere to those requirements. Don't worry about being 100% pure. Remove enough impure constructs (references or arrays) to get a feel for the algorithm in a functional way.

2) Write any part of the algorithm that you can. Leaving functions blank, add dummy values, and just implement what you know --then you can ask SO better, more informed questions.

3) Re-write it. There is a good chance you missed some functional constructs or used an array or reference where you now realize you can use a list or set or by passing an accumulator. You may have defined a list, but later you realize you cannot randomly access it (well, it would be pretty detrimental to), or it needs to be traversed forward and back (a good place for a zipper). Either way, when you finally get it you'll know, and you should have a huge ear-to-ear grin on your face.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文