编辑纯函数图

发布于 2025-01-02 13:22:41 字数 653 浏览 1 评论 0原文

假设有一个图表和一些函数集,例如:

create-node :: Graph -> (Graph, Node)
split-node :: Graph -> Node -> (Graph, Node, Node)

我想创建这些函数的版本,这些函数不需要 Graph 作为参数,主要是为了方便(最好没有 monad,所以我不会'不需要将每个图形操作代码段包装在一个 monad 块中)。那么这个呢:

create-node :: (Graph -> (Graph, Node))
split-node :: (Graph -> Node) -> ((Graph -> Node), (Graph -> Node))

或者更一般地说:

fun :: (Graph -> Argument) -> ... -> (Graph -> Result)

然后我就可以使用 (Graph -> ...) 值,就像它们是普通节点一样。最后,要从 (Graph -> ...) 值中获取真实图形,只需将其应用于空图形即可。这是一个合理的做法吗?

Let's say there is a graph and some set of functions like:

create-node :: Graph -> (Graph, Node)
split-node :: Graph -> Node -> (Graph, Node, Node)

I would like to create versions of those functions that don't expect Graph as an argument, mainly for convenience (preferably without monads so I wouldn't need to wrap every graph manipulating piece of code in a monad block). So what about this:

create-node :: (Graph -> (Graph, Node))
split-node :: (Graph -> Node) -> ((Graph -> Node), (Graph -> Node))

Or more generally:

fun :: (Graph -> Argument) -> ... -> (Graph -> Result)

I would then be able to use the (Graph -> ...) values as if they were normal nodes. In the end, to get a real graph out of a (Graph -> ...) value, just apply it to an empty graph. Is this a reasonable approach?

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

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

发布评论

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

评论(1

云仙小弟 2025-01-09 13:22:41

好的,所以

create-node :: (Graph -> (Graph, Node))

状态单子,只是没有花哨的新类型(和翻转的返回值)。所以我看不出这里不使用 State 的好处。毕竟,这让我可以使用状态 Monad 编写相当干净的代码:

 reverseEdgesM :: State Graph ()
 reverseEdgesM = do --...

然后每当我有一些纯代码可以使用 runState 和朋友运行时弹出它:

 reverseEdges :: Graph -> Graph
 reverseEdges = execState reverseEdgesM

如果你确实想推进你的函数的想法,您可能需要查看差异列表以了解它是如何使用简单列表完成的。

另外,如果您刚刚要实现一些算法,您可能需要研究现有的函数图数据结构库(例如 fgl),而不是自己滚动。如果您想了解该理论,请查看 Erwig 关于归纳图的论文

Ok, so

create-node :: (Graph -> (Graph, Node))

is the state monad, just without the fancy newtype (and a flipped return value). So I don't see the advantage of not using State here. After all that lets me write fairly clean code using the state Monad:

 reverseEdgesM :: State Graph ()
 reverseEdgesM = do --...

Then pop out of it whenever I've got some pure code to run using runState and friends:

 reverseEdges :: Graph -> Graph
 reverseEdges = execState reverseEdgesM

If you do want to push forward with your functions idea, you may want to look into difference lists to see how its done with simple lists.

Also, if you've just got some algorithms to implement, you may want to look into existing functional graph data structure libraries (like fgl), rather than rolling your own. If you want to understand the theory, check out Erwig's paper on inductive graphs.

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